linux系统编程试卷(答案) 下载本文

内容发布更新时间 : 2024/5/18 4:30:46星期一 下面是文章的全部内容请认真阅读。

出四个参数的值。

3、传给线程1参数的值必需要能够改变,传给线程2参数的值必需不能改变

#include #include #include

typedef struct{

int A; int B; int C;

}DATA;

/*传参方法1*/

void *thread_1(void *arg) { }

/*传参方法2*/

sleep(1);

rec = (DATA *)arg; rec->A += 1; rec->B += 1; rec->C += 1; return NULL; DATA *rec;

void *thread_2(void * arg) { }

int main(int argc, char *argv[]) {

test.A = 100; test.B = 100; test.C = 100; /*创建两个线程*/

pthread_create(&tid1, NULL, (void *)thread_1, (void *)(&test)); pthread_create(&tid2, NULL, (void *)thread_2, (void *)D); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf(\printf(\pthread_t tid1,tid2; DATA test; int D = 100; int rec = 0;

sleep(1);

rec = (int )(arg); rec ++; return NULL;

}

printf(\printf(\return 0;

3) 编制一段程序,实现进程的管道通信:

使用系统调用pipe()建立一条管道线,2个子进程分别向管道各写一句话: Child process 1 is sending a message! Child process 2 is sending a message!

而父进程则从管道中读出来自于2个子进程的信息,显示在屏幕上。

要求:父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。

#include #include #include #include

int main(int argc, char *argv[]) { int fd[2]; pid_t pid; pid_t pid_sec; char buf[7]; pipe(fd); if((pid = fork())<0) { printf(\ exit(1); } else if(pid == 0) { close(fd[0]); write(fd[1],\ close(fd[1]); exit(0); } else { if((pid_sec = fork())<0) {

}

printf(\ exit(1); } else if(pid_sec==0) { close(fd[0]); write(fd[1],\ close(fd[1]); exit(0); } else { wait(0); close(fd[1]); read(fd[0],buf,7); printf(\ wait(0); close(fd[1]); read(fd[0],buf,7); close(fd[0]); printf(\ exit(0); } }

return 0;