简体   繁体   中英

Segmentation Fault and semaphores not working correctly

I've been working on this assignment for school that requires me to make a program that uses 3 processes in total, and uses semaphores to count from 0 to a number specified by a argument maxnum which has to be initialized from within the main after the two children have been forked and the processes must print out the numbers in such a way that the main does numbers 0, 3, 6, the first child 1, 4, 7 and the second child 2, 5, 8. I've been having problems figuring out why my code is giving me segmentation faults when it reaches the first sem_post() command within my code. I had my code working at one point but when it ran, the order that the processes printed out the numbers was always completely random and never remotely close to right order I needed. I have been deleting the junk semaphore after failures too so that eliminates that problem. Any help in this issue would be greatly appreciated.

Note that the printfs are just there so I know which order they post, and sorry for how messy my code may be.

#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>


#define SEM_NAME "/test.mutex"

sem_t *sem1;
sem_t *sem2;
sem_t *sem3;

int main(int argc, char *argv[]){

sem1 = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1);
if(sem1 == (void *)-1){
    perror("sem_open() failed ");
}
sem2 = sem_open(SEM_NAME, O_CREAT, O_RDWR, 0);
if(sem2 == (void *)-1){
    perror("sem_open() failed ");
}
sem3 = sem_open(SEM_NAME, O_CREAT, O_RDWR, 0);
if(sem3 == (void *)-1){
    perror("sem_open() failed ");
}


int *curnum;
int *maxnum;


const int segment_size = 4096;


int segment_id = shmget(IPC_PRIVATE, segment_size,S_IRUSR|S_IWUSR);


curnum = (int *) shmat(segment_id, NULL, 0);
maxnum = curnum + 1;


if(fork()){
    if(fork()){
        *maxnum = atoi(argv[1]);
        sem_wait(sem1);
        while(*curnum <= *maxnum){
            printf("%d", *curnum);
            printf(" Main \n");
            ++curnum[0];
            sem_post(sem2);
            sem_wait(sem1);
        }
        printf("Exiting Main Loop");
        sem_post(sem2);
        wait();
        shmctl(segment_id, IPC_RMID, NULL);
    }
    else{
        sem_wait(sem3);
        while(*curnum <= *maxnum){
            printf("%d", *curnum);
            printf(" Child 1\n");
            ++curnum[0];
            sem_post(sem1);
            sem_wait(sem3);
        }
        printf("Exiting Child 1 Loop");
        sem_post(sem1);
    }
}
else{
    sem_wait(sem2);
    while(*curnum <= *maxnum){
        printf("%d", *curnum);
        printf(" Child 2\n");
        ++curnum[0];
        sem_post(sem3);
        sem_wait(sem2);
    }
    printf("Exiting Child 2 Loop");
    sem_post(sem3);
}

sem_close(sem1);
sem_close(sem2);
sem_close(sem3);

sem_unlink(SEM_NAME);


/* remove shared memory segment */
//shmctl(segment_id, IPC_RMID, NULL);

return 0;
}

I just fixed my program so it works properly, I created two more SEM_NAME definitions and used those to store sem2 and sem3. I changed the while loops to do while loops, and removed most of the sem_post calls and it works properly now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM