2 years ago

#63767

test-img

periKor

C POSIX semaphores for processes bug

Im new to semaphores in C and I am making a programm to solve the writers readers problem for multiple writers and readers (My code isn't very clean so sory for that).First I initialize the semaphores then I make three child processes and get their pids in the writers array and the same for the readers.

    sem_t *read;
    shmID= shmget(IPC_CREAT,sizeof(sem_t),0666|IPC_CREAT);
    read=(sem_t*)shmat(shmID,NULL,0);
    sem_init(read,1,0);
    sem_t *write;
    shmID= shmget(IPC_CREAT,sizeof(sem_t),0666|IPC_CREAT);
    write=(sem_t*)shmat(shmID,NULL,0);
    sem_init(write,1,1);

    shmID=shmget(IPC_PRIVATE,sizeof(wr),0666|IPC_CREAT);
    void* shmem=(int*)shmat(shmID,NULL,0);
    int* writers;
    writers=(int*)shmem;
    shmID=shmget(IPC_PRIVATE,sizeof(re),0666|IPC_CREAT);
    void* sshmem=(int*)shmat(shmID,NULL,0);
    int* readers;
    readers=(int*)sshmem;

///make writers
    int pid=fork();
    if(pid>0)
        writers[0]=pid;
    for (int i=0;i<w-1;i++){
        if(pid>0){
            pid=fork();
            if(getpid()>ppid)
                writers[i+1]=getpid();
        }


        wait(NULL);

    }

    ///make readers
    if(getpid()==ppid){
        pid=fork();
        if(pid>0)
            readers[0]=pid;
        for (int i=0;i<r-1;i++){



            if(pid>0){

                pid=fork();
                if(getpid()>ppid)
                    readers[i+1]=getpid();
            }


            wait(NULL);

        }
    }

Then I write the code for readers and writers

///code for writers
    if(is_writer(getpid(),w,writers)){
        int tmp;
        sem_getvalue(write,&tmp);
        printf("1write-%d-%d\n",tmp,getpid() );

        sem_wait(write);

        sem_getvalue(write,&tmp);
        printf("2write-%d-%d\n",tmp,getpid() );

        *p=getpid();
        mem++;

        sem_getvalue(read,&tmp);
        printf("3read-%d-%d\n",tmp,getpid() );
        sem_post(read);
        sem_getvalue(read,&tmp);
        printf("4read-%d-%d\n",tmp,getpid() );

    }

    ///code for readers
    if(is_reader(getpid(),r,readers)){

        int tmp;
        sem_getvalue(read,&tmp);
        printf("1read-%d\n",tmp );
        sem_wait(read);
        sem_getvalue(read,&tmp);
        printf("2read-%d\n",tmp );
        mem=*p;

        sem_getvalue(write,&tmp);
        printf("3write-%d\n",tmp );
        sem_post(write);
        sem_getvalue(write,&tmp);
        printf("4write-%d\n",tmp );
    }

if i run the code i get this output

1write-1-14329
2write-0-14329
3read-0-14329
4read-1-14329
1write-1-14330
2write-0-14330
3read-0-14330
4read-1-14330
1write-1-14331
2write-0-14331
3read-0-14331
4read-1-14331
1read-1
2read-0
3write-0
4write-1
1read-1
2read-0
3write-0
4write-1
1read-1
2read-0
3write-0
4write-1

line 1: the write semaphore is 1 Line 2: the semaphore write gets 0 after sem_wait() Line 5: the write is again 1 but no sem_post() has been call I also print the pid of the processes. the process changees between line 1 and 5.This makes me belive that between the processes the semaphores are not sheared.Why is that? I used shmget() to make the semaphores shared between the processes.Why they are not sheared?

c

linux

posix

0 Answers

Your Answer

Accepted video resources