In previous lesson, I introduced some functions of semaphore, like semget( ), semctl( ). There is another function semop( ).
This function is used to communicate between parents and child process.
Prototype:
int semop(int semid, struct sembuf *sops, size_t nsops)
The semid argument is the semaphore ID returned by a previous semget() call.
The sops argument is a pointer of a structure, sembuf.
The nsops argument specifies the length of the array.
The sembuf structure specifies a semaphore operation, as defined in <sys/sem.h>.
struct sembuf {
ushort_t sem_num; /* semaphore number */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
};
Now, there is a problem to understand that what operation will done ?
There are 3 types of operations.
- A positive integer increments the semaphore value by that amount.
- A negative integer decrements the semaphore value by that amount.
- A value of zero means to wait for the semaphore value to reach zero.
OH ! Too much description. So, there is an example that help you to understand.
// **********************************************************************
// **********************************************************************
// Write " ipcs -s " in Terminal to see semaphore list
// Here we set 1 in semaphore at parent. So, child will wait until it
// get 0 at semaphore
// when we again set 0 at parent, then child will execute
// **********************************************************************
// **********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <error.h>
int main()
{
int semid, pid;
struct sembuf sop;
semid = semget( 0x20 , 1, IPC_CREAT | 0666 ) ;
pid = fork();
if( pid ==0 ){
sleep(2);
printf("child \n");
sop.sem_num =0;
sop.sem_op =0; /// set value 0
sop.sem_flg =0;
semop ( semid, &sop , 1 );
printf(" child over \n");
}
else{
printf("parent \n");
semctl ( semid, 0, SETVAL, 1 ); /// as set value 1, child will wait
printf("parent sleep\n");
sleep(5);
printf("parent second\n");
semctl ( semid, 0, SETVAL, 0 ); /// now set value 0, so child will not wait now
printf("parent end\n");
}
return 0;
}
// **********************************************************************
// **********************************************************************
This function is used to communicate between parents and child process.
Prototype:
int semop(int semid, struct sembuf *sops, size_t nsops)
The semid argument is the semaphore ID returned by a previous semget() call.
The sops argument is a pointer of a structure, sembuf.
The nsops argument specifies the length of the array.
The sembuf structure specifies a semaphore operation, as defined in <sys/sem.h>.
struct sembuf {
ushort_t sem_num; /* semaphore number */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
};
Now, there is a problem to understand that what operation will done ?
There are 3 types of operations.
- A positive integer increments the semaphore value by that amount.
- A negative integer decrements the semaphore value by that amount.
- A value of zero means to wait for the semaphore value to reach zero.
OH ! Too much description. So, there is an example that help you to understand.
// **********************************************************************
// **********************************************************************
// Write " ipcs -s " in Terminal to see semaphore list
// Here we set 1 in semaphore at parent. So, child will wait until it
// get 0 at semaphore
// when we again set 0 at parent, then child will execute
// **********************************************************************
// **********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <error.h>
int main()
{
int semid, pid;
struct sembuf sop;
semid = semget( 0x20 , 1, IPC_CREAT | 0666 ) ;
pid = fork();
if( pid ==0 ){
sleep(2);
printf("child \n");
sop.sem_num =0;
sop.sem_op =0; /// set value 0
sop.sem_flg =0;
semop ( semid, &sop , 1 );
printf(" child over \n");
}
else{
printf("parent \n");
semctl ( semid, 0, SETVAL, 1 ); /// as set value 1, child will wait
printf("parent sleep\n");
sleep(5);
printf("parent second\n");
semctl ( semid, 0, SETVAL, 0 ); /// now set value 0, so child will not wait now
printf("parent end\n");
}
return 0;
}
// **********************************************************************
// **********************************************************************
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন