-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-process.c
58 lines (53 loc) · 973 Bytes
/
demo-process.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREADS 2
void *PrintHello(void *args)
{
int thread_arg;
sleep(1);
thread_arg = (int)(*((int*)args));
printf("Hello from thread %d\n", thread_arg);
return NULL;
}
void creat_thread(void)
{
int rc,t;
pthread_t thread[NUM_THREADS];
for( t = 0; t < NUM_THREADS; t++)
{
printf("Creating thread %d\n", t);
rc = pthread_create(&thread[t], NULL, PrintHello, &t);
if (rc)
{
printf("ERROR; return code is %d\n", rc);
return;
}
}
sleep(5);
for( t = 0; t < NUM_THREADS; t++)
pthread_join(thread[t], NULL);
}
int main()
{
pid_t pid;
if((pid=fork())==0)
{
//子进程代码
creat_thread();
printf("子进程%d\n", pid);
exit(0);
}
else if(pid>0)
{
//父进程代码
creat_thread();
printf("父进程%d\n", pid);
exit(0);
}
else {
printf("Error");
exit(1);
}
}