Home > OS >  light
light

Time:10-25

# include & lt; Stdio. H>;
# include & lt; Unistd. H>;
# include & lt; Limits. H>;
# include & lt; Errno. H>;
# include & lt; String. H>;
# include & lt; Stdlib. H>;
# include & lt; Sys/stat. H>;
# include & lt; Sys/wait. H>;
# include & lt; Sys/ipc. H>;
# include & lt; Sys/sem. H>;
# define PERMS S_IRUSR | S_IWUSR
Void init_semaphore_struct (struct sembuf * sem, int semnum,
Int semop, int semflg)
{
/* initial words light structure */
Sem - & gt;; Sem_num=semnum;
Sem - & gt;; Sem_op=semop;
Sem - & gt;; Sem_flg=semflg;
}
Int del_semaphore (int semid)
{
/* semaphore is not deleted along with the end of the program, if we didn't remove (1 to 0)
Can use the ipcs command to see the light, use ipcrm can delete lights
*/
If # 1
Return semctl (semid, 0, IPC_RMID);
# endif
}
Int main (int arg c, char * * argv)
{
Char buffer [MAX_CANON], * c.
Int I, n;
Int semid, semop_ret status;
Pid_t childpid;
Struct sembuf semwait semsignal;
If ((arg c! ((n=2) | |=atoi (argv [1])) & lt; 1))
{
Fprintf (stderr, "number Usage: % s \ n \ a", argv [0]).
exit(1);
}
/* use IPC_PRIVATE said by the system to choose a keyword to create */
/* create light after the initial value of 0 */
If ((semid=semget (IPC_PRIVATE, 1, PERMS))==1)
{
Fprintf (stderr, "[% d] : Acess Semaphore Error: % s \ n \ a",
Getpid () and strerror (errno));
The exit (1)