EmbLogic's Blog

semaphore

ead    1.1;
access;
symbols;
locks
root:1.1; strict;
comment    @ * @;

1.1
date    2014.05.14.23.41.10;    author root;    state Exp;
branches;
next    ;

desc
@semaphore with semget,semctl,semop.
@

1.1
log
@Initial revision
@
text
@#include<stdio.h>
#include<linux/sem.h>
#include<stdlib.h>
int main()
{
int i,aa,aaa;
union semun u;
u.val=1;
struct sembuf sem1;
sem1.sem_num=0;
sem1.sem_op=-1;
sem1.sem_flg=SEM_UNDO;

aa=semget((key_t)1234,1,IPC_CREAT|0777);
if(aa<0)
{
perror(“semget failure\n”);
goto OUT;
}
aaa=semctl(aa,0,SETVAL,0);
if(aaa<0)
{
perror(“segctl failure\n”);
goto OUT;
}
semop(aa,&sem1,0);
for(i=1;i<=3;i++)
{
printf(“hello i m into critical section =%d\n”,getpid());
sleep(2);
printf(” i m into critical section =%d\n”,getpid());
sleep(2);
printf(“bye i m into critical section =%d\n”,getpid());

}

sem1.sem_num=0;
sem1.sem_op=1;
sem1.sem_flg=SEM_UNDO;
semop(aa,&sem1,0);
return 0;
OUT:
return -1;
}
@

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>