-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
#include <sys/ipc.h> | ||
#include <sys/sem.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <string.h> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "main.h" | ||
|
||
#define KEY 0x1111 | ||
|
||
union semun { | ||
int value; | ||
struct semid_ds *buf; | ||
unsigned short *arr; | ||
}; | ||
|
||
struct sembuf a = {0, -1, SEM_UNDO}; | ||
struct sembuf b = {0, +1, SEM_UNDO}; | ||
|
||
int main(void) | ||
{ | ||
int id = semget(KEY, 1, 0666 | IPC_CREAT); | ||
if (id < 0) { | ||
perror("semget\n"); | ||
exit(0); | ||
} | ||
union semun su; | ||
su.value = 1; | ||
|
||
if(semctl(id, 0, SETVAL, su) < 0) { | ||
perror("semctl error"); | ||
exit(0); | ||
} | ||
|
||
int pid; | ||
|
||
pid = fork(); | ||
srand(pid); | ||
|
||
if (pid < 0) { | ||
perror("fork error\n"); | ||
exit(0); | ||
} else if (pid) { | ||
char *se = "abc"; | ||
int l = strlen(se); | ||
|
||
for (int i = 0; i < l; i++) { | ||
if (semop(id, &a, 1) < 0) { | ||
perror("semop error with a\n"); | ||
exit(0); | ||
} | ||
putchar(se[i]); | ||
fflush(stdout); | ||
sleep(1); | ||
putchar(se[i]); | ||
fflush(stdout); | ||
if (semop(id, &b, 1) < 0) { | ||
perror("semop error with b\n"); | ||
exit(0); | ||
} | ||
|
||
sleep(1); | ||
} | ||
} else { | ||
char *se = "abc"; | ||
int l = strlen(se); | ||
|
||
for (int i = 0; i < l; i++) { | ||
if (semop(id, &a, 1) < 0) { | ||
perror("semop error with a\n"); | ||
exit(0); | ||
} | ||
putchar(se[i]); | ||
fflush(stdout); | ||
sleep(1); | ||
putchar(se[i]); | ||
fflush(stdout); | ||
if (semop(id, &b, 1) < 0) { | ||
perror("semop error with b\n"); | ||
exit(0); | ||
} | ||
|
||
sleep(1); | ||
} | ||
} | ||
return 0; | ||
} |