[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
ςοδιτεμψσλικ πςογεσσ, shmp_mm.c :
1 #include "registration.h"
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/ipc.h>
7 #include <sys/sem.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <wait.h>
11 #include <errno.h>
12
13 #define FILENAME "./class"
14
15 static struct CLASS class = {
16 "1001",
17 "120191",
18 "C Language for Programmers",
19 15 };
20 #define NCHILD 3
21 static pid_t child[NCHILD];
22
23 static int semid, fd;
24 static char ascsemid[10];
25
26 static char pname[14];
27 static void rpterror(char *),Σlass_init(void),
28 sem_init(void), wait_and_wrap_up(void);
29
30 main(int argc, char *argv[])
31 {
32 int i;
33
34 strcpy(pname,argv[0]);
35 class_init();
36 sem_init();
37 for(i=0; i<NCHILD; i++){
38 child[i] = fork();
39 switch(child[i]){
40 case -1:
41 rpterror("fork-failure");
42 exit(1);
43 case 0:
44 sprintf(pname,"shmc%d",i+1);
45 execl("shmc_mm", pname, FILENAME,
46 ascsemid, (char*)0);
47 rpterror("execl failed");
48 exit(2);
49 }
50
51 }
52 wait_and_wrap_up();
53 }
54
55 static void class_init(void)
56 {
57 fd = open(FILENAME, O_WRONLY | O_CREAT | O_EXCL, 0666);
58 if(fd == -1)
59 if( errno == EEXIST ){
60 printf(FILENAME " exists, starting with presentcount\n");
61 return;
62 }
63 else {
64 rpterror("open failed");
65 exit(3);
66 }
67
68 if( write( fd, &class, sizeof(class)) < sizeof(class)) {
69 rpterror("write of structure failed");
70 exit(4);
71 }
72 }
73
74 static void sem_init(void)
75 {
76 if((semid=semget(IPC_PRIVATE,1,0600 | IPC_CREAT))== -1){
77 rpterror("semget failed");
78 exit(5);
79 }
80 if((semctl(semid,0,SETVAL,1)) == -1){
81 rpterror("parent: semctl, SETVAL failed");
82 exit(6);
83 }
84 sprintf(ascsemid, "%d", semid);
85 }
86
87 static void wait_and_wrap_up(void)
88 {
89 pid_t wait_rtn; int w, ch_active = NCHILD;
90 while( ch_active > 0 ){
91 wait_rtn = wait( (int *)0 );
92 for(w=0; w<NCHILD; w++)
93 if(child[w]==wait_rtn){
94 ch_active--;
95 break;
96 }
97 }
98 printf("Parent removing data file and sem\n");
99 unlink(FILENAME);
100 semctl(semid, 0, IPC_RMID, 0);
101 exit(0);
102 }
103
104 static void rpterror(char *string)
105 {
106 char errline[50];
107 sprintf(errline,"%s %s", string, pname);
108 perror(errline);
109 }
ποδπςογεσσ - πςοδαχεγ νεστ χ λμασσε, shmc_mm.c :
1 #include "registration.h"
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/ipc.h>
6 #include <sys/sem.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 static struct CLASS *class_ptr;
11 static char *pname;
12 static int fd, semid, ret;
13 static struct sembuf lock = { 0, -1, 0};
14 static struct sembuf unlock = { 0, 1, 0};
15 static void sell_seats(void), rpterror(char *);
16
17 main(int argc, char *argv[])
18 {
19 if(argc < 3){
20 fprintf(stderr,"Usage: %s shmid semid\n", argv[0]);
21 exit(1);
22 }
23 pname = argv[0];
24 if((fd = open(argv[1], O_RDWR)) == -1) {
25 rpterror("open failed");
26 exit(2);
27 }
28
29 class_ptr = (struct CLASS *) mmap(0,sizeof (struct CLASS),
30 PROT_READ|PROT_WRITE,MAP_SHARED, fd,0);
31 if(class_ptr == (struct CLASS *)-1){
32 rpterror("mmap failed");
33 exit(4);
34 }
35
36 sscanf(argv[2], "%d", &semid);
37
38 sell_seats();
39
40 munmap(class_ptr);
41 exit(0);
42 }
43
44 static void sell_seats(void){
45 int all_out = 0;
46 srand( (unsigned)getpid());
47 while ( !all_out ){ /* loop to sell all seats */
48 if(semop(semid,&lock,1) == -1){
49 rpterror("semop lock failed");
50 exit(4);
51 }
52 if (class_ptr->seats_left > 0){
53 class_ptr->seats_left--;
54 printf("%s SOLD SEAT -- %2d left\n",
55 pname,class_ptr->seats_left);
56 }
57 else{
58 all_out++;
59 printf("%s sees no seats left\n", pname);
60 }
61 ret = semop(semid,&unlock,1);
62 if (ret == -1) {
63 rpterror("semop unlock failed");
64 exit(5);
65 }
66 sleep( (unsigned)rand()%10 + 1);
67 }
68 }
69
70 static void rpterror(char *string)
71 {
72 char errline[50];
73 sprintf(errline,"%s %s", string, pname);
74 perror(errline);
75 }