112 lines
2.1 KiB
C
112 lines
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
|
|
void *func1(void *data)
|
|
{
|
|
int i;
|
|
char *string;
|
|
|
|
string = (char *)data;
|
|
|
|
for (i = 0; i < 10; i++)
|
|
{
|
|
fprintf(stderr, "[%d] --> %s\n", i, string);
|
|
}
|
|
}
|
|
|
|
void *func2(void *data)
|
|
{
|
|
int i;
|
|
char *string;
|
|
|
|
string = (char *)data;
|
|
for (i = 0; i < 10; i++)
|
|
{
|
|
fprintf(stderr, "--[%d] --> %s\n", i, string);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int pid, pid2;
|
|
int status1 = 0;
|
|
int status2 = 0;
|
|
int pstatus1;
|
|
int pstatus2;
|
|
int c1 = 0;
|
|
int pth_id;
|
|
int retval = -1;
|
|
char *str1 = "RyuHyunSoo";
|
|
char *str2 = "RooluAndJin";
|
|
|
|
pthread_t pthr_t[2];
|
|
|
|
if ( (pid = fork()) < 0 )
|
|
{
|
|
fprintf(stderr, "Cannot Fork(1)!!\n");
|
|
}
|
|
else if ( pid == 0 )
|
|
{
|
|
fprintf(stderr, "Start Child(one) Process!!\n");
|
|
// scanf("%d", &c1);
|
|
// fprintf(stderr, "Child(one) Result is : %d\n", c1);
|
|
|
|
if (pthread_create(&pthr_t[0], NULL, func1, (void *)str1))
|
|
{
|
|
perror("thread of Child(one) create error:");
|
|
exit(0);
|
|
}
|
|
else
|
|
fprintf(stderr, "Success!! pth_id1 = %d\n", pthr_t[0]);
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Mother's job with Child(one)\n");
|
|
|
|
if ( (pid2=fork()) == 0 )
|
|
{
|
|
fprintf(stderr, "Start Child(two) Process!!\n");
|
|
// scanf("%d", &c1);
|
|
// fprintf(stderr, "Child(two) Result is : %x\n", c1);
|
|
|
|
if (pthread_create(&pthr_t[1], NULL, func2, (void *)str2))
|
|
{
|
|
perror("thread of Child(two) create error:");
|
|
exit(0);
|
|
}
|
|
else
|
|
fprintf(stderr, "Success!! pth_id2 = %d\n", pthr_t[1]);
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Mothers' job with Child(two) !!\n");
|
|
wait(&status1);
|
|
}
|
|
wait(&status2);
|
|
}
|
|
|
|
/* Ending .... */
|
|
if (pid == 0 || pid2 == 0)
|
|
{
|
|
fprintf(stderr, "End Child Process!!..(pid=%d)\n", pid);
|
|
|
|
if (pid == 0)
|
|
{
|
|
retval = pthread_join(pthr_t[0], (void **)&pstatus1);
|
|
fprintf(stderr, "Thread of Child(one) End %d(%d)\n", (pthread_t)pthr_t[0], retval);
|
|
}
|
|
else if (pid2 == 0)
|
|
{
|
|
retval = pthread_join(pthr_t[1], (void **)&pstatus2);
|
|
fprintf(stderr, "Thread of Child(two) End %d(%d)\n", (pthread_t)pthr_t[1], retval);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "End Mother Process!!..(pid=%d)\n", pid);
|
|
}
|
|
exit(0);
|
|
}
|