IPC Programming in Unix
Results 1 to 5 of 5

Thread: IPC Programming in Unix

  1. #1
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004
    Posts
    258

    Arrow IPC Programming in Unix

    Hey ,

    I'm trying to implement a semaphore which provides mutual exclusion among two processes to a given critical section on Linux. Below is the code which does that. I had written the code initially without the lines commented as "Check1" and "Check2". The code could not provide any mutual exclusion. I added the two lines explained previously for debugging purposes. After adding the lines, the code started giving the required output. I can't figure out what happened. Commenting the lines and running the code leads to the previous undesired output.



    Code:
    #include<sys/sem.h>
    #include<sys/ipc.h>
    #include<stdio.h>
    #include<unistd.h>
    
    
    
    int s;
    
    signal()
    {
            printf("\ns:%d here \n",getpid()); // Check 1
            struct sembuf sa={0,1,0};
            semop(s,&sa,1);
     }
    
     wait()
    {
     
            printf("\nw:%d here \n",getpid()); //Check 2
            struct sembuf sa={0,-1,0};
            semop(s,&sa,1);
    }
    
    
    void cs() // Critical Section
    {
      	
            printf("\nProcess%d entering ",getpid());
            sleep(1);
            printf("\nProcess%d exiting ",getpid());
            sleep(1);
    }
    
    
    
    
    union semun   // The Union Structure for semget
    {
            int val;
    	struct semid_ds *buff;
    	unsigned short int *array;
    }
    
    main()
    {       int i;
    	union semun x;
    	int pid;
    	s=semget(IPC_PRIVATE,1,0666|IPC_CREAT); // Creating a single semaphore
            x.val=1;
    	semctl(s,0,SETVAL,x);   // Initializing semaphore's value to be 1
    	pid=fork();
    	for(i=0;i<2;i++)
    	{       
    	              wait(); // Decrement Semaphore
    	     	       cs();
    		      signal(); // Increment Semaphore
    		       sleep(1);
    		      
             }
    
    
    }


    Does it ring a bell ?...

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    I'm not getting the right output with those lines in there. I would try and help but using that stuff is completely foreign to me. I just wanted to let you know I don't think the output is right even with those lines, and maybe it was a weird coincidence for you.

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Posts
    420
    I'm not sure (its been awhile since I've done this level of coding) maybe you need to flush the buffer. If you replace printf with fflush(); are the results the same?

    Cheers,
    -D
    If you spend more on coffee than on IT security, you will be hacked. What\'s more, you deserve to be hacked.
    -- former White House cybersecurity adviser Richard Clarke

  4. #4
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004
    Posts
    258
    Skiddieleet: I do realize that the lines are of no significance. But the output is correct as far as mutual exclusion is concerned. I've tried many times. The process scheduler is indeed a wild beast

    dspeidel: I'm away from my pc now. Shall do it ASAP :-). Thanks anyway.

  5. #5
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004
    Posts
    258
    I've modified the code to make
    Code:
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/sem.h>
    #include<stdio.h>
    int s;
    void wait()
    {       struct sembuf w={0,-2,0};
    	printf("\nSemval of %d before wait is %d",getpid(),semctl(s,0,GETVAL)); 
    		semop(s,&w,1);
    	printf("\nnSemval of %d after wait is %d",getpid(),semctl(s,0,GETVAL));
    }
    void sigz()
    {
    	struct sembuf w={0,+2,0};
    	printf("\nSemval of %d before wait is %d",getpid(),semctl(s,0,GETVAL));
    	semop(s,&w,1);
    /*mark1*/printf("\nSemval of %d after signal is %d" , getpid(),semctl (s,0,GETVAL));  
    
    }
    void cs()
    {
    	printf("\nProcess %d entering cs",getpid());
    	sleep(2);
    	printf("\nProcesss %d quiting cs",getpid());
    	sleep(2);
    }
    
    union semnu
    {
    	int val;
    	struct semid_ds *buf;
    	unsigned short *array;
    }
    main()
    {
    	int i;
    
    	union semnu ss;
    	s=semget(IPC_PRIVATE,1,0666|IPC_CREAT);
    	ss.val=2;
    	semctl(s,0,SETVAL,ss);
    	fork();
    	for(i=0;i<3;i++)
    	{ 
                    wait();
    		cs();
    		sigz();
    		sleep(2);
    	}
    }

    The Output if fine. But the debugging results are strange. Even after doing a signal on the semaphore, the value is still the same ( the line is commented as mark1 ) . And strangely processes is able to enter the critical section inspite of the 0 value of the semaphore !.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides