Skip to content

Commit 2e49d03

Browse files
update bài 2, bài 1
Bài 1: Xóa phần debug Bài 2: Tạo 2 tiến trình con, xử lý semaphore
1 parent 6b2fe2d commit 2e49d03

File tree

6 files changed

+127
-118
lines changed

6 files changed

+127
-118
lines changed

bai1.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@
77
int x=0;
88
void* thread(void* arg)
99
{
10-
11-
printf("\n Entered..\n");
12-
1310
//critical section
1411
do{
1512
x=x+1;
1613
if(x==20)
1714
x=0;
1815
usleep(100);
1916
}while(1);
20-
21-
//signal
22-
printf("\nJust Exiting...\n");
2317
}
2418

2519
int main()

bai1_sem.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
sem_t mutex;
77
int x=0;
88
void* thread(void* arg)
9-
{
10-
//wait
11-
sem_wait(&mutex);
12-
printf("\nP1: Entered..\n");
13-
9+
{
10+
sem_wait(&mutex);
1411
//critical section
1512
do{
1613
//printf("\n%d",x);
@@ -19,10 +16,7 @@ void* thread(void* arg)
1916
x=0;
2017
//usleep(100);
2118
}while(1);
22-
23-
//signal
24-
printf("\nJust Exiting...\n");
25-
sem_post(&mutex);
19+
sem_post(&mutex);
2620
}
2721

2822

bai2

190 Bytes
Binary file not shown.

bai2.c

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,71 @@
1-
#include <sys/types.h>
2-
#include <unistd.h>
3-
#include <stdio.h>
41
#include <pthread.h>
5-
#include <semaphore.h>
6-
#include <time.h>
2+
#include <sys/stat.h>
3+
#include <stdio.h> /* printf() */
4+
#include <stdlib.h> /* exit(), malloc(), free() */
5+
#include <unistd.h>
6+
#include <sys/types.h> /* key_t, sem_t, pid_t */
7+
#include <sys/wait.h>
8+
#include <sys/shm.h> /* shmat(), IPC_RMID */
9+
#include <errno.h> /* errno, ECHILD */
10+
#include <semaphore.h> /* sem_open(), sem_destroy(), sem_wait().. */
11+
#include <fcntl.h> /* O_CREAT, O_EXEC */
12+
713
void* A1(void* arg)
814
{
9-
printf("A1");
15+
printf("A1\n");
1016
}
1117
void* A2(void* arg)
1218
{
13-
printf("A2");
19+
printf("A2\n");
1420
}
1521
void* B1(void* arg)
1622
{
17-
printf("B1");
23+
printf("B1\n");
1824
}
1925
void* B2(void* arg)
2026
{
21-
printf("B2");
27+
printf("B2\n");
2228
}
2329
int main()
2430
{
25-
printf("\n");
31+
int i;
2632
pthread_t t1, t2;
2733
pid_t pid;
28-
pid = fork();// duplicates process
29-
switch (pid)
30-
{
31-
case -1:
32-
printf( "Khong the tao tien trinh con !" );
33-
exit(1);
34-
case 0:
35-
pthread_create(&t1,NULL,A1,NULL);
36-
pthread_create(&t2,NULL,A2,NULL);
37-
pthread_join(t1, NULL);
38-
pthread_join(t2, NULL);
39-
break;
40-
default:
41-
pthread_create(&t1,NULL,B1,NULL);
42-
pthread_create(&t2,NULL,B2,NULL);
43-
pthread_join(t1, NULL);
44-
pthread_join(t2, NULL);
45-
break;
46-
}
47-
printf("\n");
48-
exit(0);
49-
//return 0;
34+
35+
/* create 2 child processes */
36+
for (i = 0; i < 2; i++){
37+
pid = fork ();
38+
if (pid < 0) /* check for error */
39+
printf ("Fork error.\n");
40+
else if (pid == 0)
41+
break; /* child processes */
42+
}
43+
44+
if (pid != 0){
45+
/* wait for all children to exit */
46+
while (pid = waitpid (-1, NULL, 0)){
47+
if (errno == ECHILD)
48+
break;
49+
}
50+
51+
printf ("\nParent: All children have exited.\n");
52+
exit (0);
53+
}
54+
else{
55+
if(i==0){
56+
pthread_create(&t1,NULL,A1,NULL);
57+
pthread_create(&t2,NULL,A2,NULL);
58+
pthread_join(t1, NULL);
59+
pthread_join(t2, NULL);
60+
61+
} else{
62+
pthread_create(&t1,NULL,B1,NULL);
63+
pthread_create(&t2,NULL,B2,NULL);
64+
pthread_join(t1, NULL);
65+
pthread_join(t2, NULL);
66+
}
67+
exit (0);
68+
69+
}
5070
}
5171

bai2_sem

373 Bytes
Binary file not shown.

bai2_sem.c

Lines changed: 72 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,93 @@
1-
#include <sys/types.h>
2-
#include <unistd.h>
3-
#include <stdio.h>
41
#include <pthread.h>
5-
#include <semaphore.h>
6-
#include <sys/types.h>
72
#include <sys/stat.h>
8-
#include <fcntl.h>
3+
#include <stdio.h> /* printf() */
4+
#include <stdlib.h> /* exit(), malloc(), free() */
5+
#include <unistd.h>
6+
#include <sys/types.h> /* key_t, sem_t, pid_t */
7+
#include <sys/wait.h>
8+
#include <sys/shm.h> /* shmat(), IPC_RMID */
9+
#include <errno.h> /* errno, ECHILD */
10+
#include <semaphore.h> /* sem_open(), sem_destroy(), sem_wait().. */
11+
#include <fcntl.h> /* O_CREAT, O_EXEC */
912

10-
#define SNAME1 "/mysem1"
11-
#define SNAME2 "/mysem2"
12-
sem_t mutex1, mutex2;
1313
void* A1(void* arg)
1414
{
15-
printf("A1");
16-
//post A2
17-
sem_post(&mutex1);
18-
//Post B2
19-
sem_t* sem1 = (sem_t*) arg;
20-
sem_post(sem1);
15+
printf("A1\n");
2116
}
2217
void* A2(void* arg)
2318
{
24-
//Wait A1
25-
sem_wait(&mutex1);
26-
//Wait B1
27-
sem_t* sem2 = (sem_t*) arg;
28-
sem_wait(sem2);
29-
30-
printf("A2");
19+
printf("A2\n");
3120
}
3221
void* B1(void* arg)
3322
{
34-
printf("B1");
35-
//Post B2
36-
sem_post(&mutex2);
37-
//Post A2
38-
sem_t* sem2 = (sem_t*) arg;
39-
sem_post(sem2);
40-
23+
printf("B1\n");
4124
}
4225
void* B2(void* arg)
4326
{
44-
//Wait B1
45-
sem_wait(&mutex2);
46-
//Wait A1
47-
sem_t* sem1 = (sem_t*) arg;
48-
sem_wait(sem1);
49-
printf("B2");
27+
printf("B2\n");
5028
}
5129
int main()
52-
{
53-
printf("\n");
54-
sem_init(&mutex1, 0, 0);
55-
sem_init(&mutex1, 0, 0);
30+
{
31+
unsigned int value = 0;
32+
int i;
5633
sem_t *sem1, *sem2;
57-
//sem_init(sem1, 1, 0);
58-
//sem_init(sem2, 1, 0);
59-
pthread_t t1, t2, t3, t4;
34+
sem1 = sem_open ("pSem1", O_CREAT | O_EXCL, 0644, value);
35+
sem2 = sem_open ("pSem2", O_CREAT | O_EXCL, 0644, value);
36+
/* name of semaphore is "pSem", semaphore is reached using this name */
37+
sem_unlink ("pSem1");
38+
sem_unlink ("pSem2");
39+
pthread_t t1, t2;
6040
pid_t pid;
61-
pid = fork();
62-
switch (pid)
63-
{
64-
case -1:
65-
printf( "Khong the tao tien trinh con !" );
66-
exit(1);
67-
case 0:
68-
printf("\n");
69-
sem1 = sem_open(SNAME1, 0);
70-
sem2 = sem_open(SNAME2, 0);
71-
pthread_create(&t1,NULL,A1,(void*)&sem1);
72-
pthread_create(&t2,NULL,A2,(void*)&sem2);
73-
pthread_join(t2, NULL);
41+
42+
/* fork child processes */
43+
for (i = 0; i < 2; i++){
44+
pid = fork ();
45+
if (pid < 0) /* check for error */
46+
printf ("Fork error.\n");
47+
else if (pid == 0)
48+
break; /* child processes */
49+
}
50+
51+
if (pid != 0){
52+
/* wait for all children to exit */
53+
while (pid = waitpid (-1, NULL, 0)){
54+
if (errno == ECHILD)
55+
break;
56+
}
57+
58+
printf ("\nParent: All children have exited.\n");
59+
60+
/* cleanup semaphores */
61+
printf("sem_destroy return value:%d\n",sem_destroy (sem1));
62+
printf("sem_destroy return value:%d\n",sem_destroy (sem2));
63+
exit (0);
64+
}
65+
else{
66+
printf("Hello from process %d\n",i);
67+
if(i%2==0){
68+
//wait
69+
printf("P %d is waiting...\n",i);
70+
pthread_create(&t1,NULL,A1,NULL);
7471
pthread_join(t1, NULL);
75-
break;
76-
default:
77-
printf("\n");
78-
sem1 = sem_open(SNAME1, 0);
79-
sem2 = sem_open(SNAME2, 0);
80-
pthread_create(&t3,NULL,B1,(void*)&sem2);
81-
pthread_create(&t4,NULL,B2,(void*)&sem1);
82-
pthread_join(t4, NULL);
83-
pthread_join(t3, NULL);
84-
break;
85-
}
86-
printf("\n");
87-
sem_destroy(&mutex1);
88-
sem_destroy(&mutex2);
89-
exit(0);
90-
//return 0;
72+
sem_post(sem1);
73+
sem_wait(sem2);
74+
pthread_create(&t2,NULL,A2,NULL);
75+
pthread_join(t2, NULL);
76+
77+
//sleep (1);
78+
} else{
79+
pthread_create(&t1,NULL,B1,NULL);
80+
pthread_join(t1, NULL);
81+
82+
sem_post(sem2);
83+
sem_wait(sem1);
84+
pthread_create(&t2,NULL,B2,NULL);
85+
pthread_join(t2, NULL);
86+
87+
}
88+
exit (0);
89+
90+
}
91+
9192
}
9293

0 commit comments

Comments
 (0)