验证性实验-------线程间通信
#include “stdio.h”
#include “stdlib.h”
#include “pthread.h”
#include <unistd.h>
void *fun(void var){ //子线程
int j;
for (j = 0; j < 10; j++) {
usleep(100);
printf(“this is child thread %s, j =%d\n”,(char)var,j);
}
pthread_exit(“Thank you for the CPU time”);
}
int main(){ //主线程
int i;
char str[] = " hello linux\n";
pthread_t tid; //声明线程ID变量,数据类型必须是pthread_t
int ret; //线程创建函数返回值
void *thread_result;
ret = pthread_create(&tid, NULL, fun, (void *)str); //创建子线程
if(ret<0){
printf(" create thread failure\n");
return -1;
}
for (i = 0; i < 10; i++) {
usleep(100);
printf("this is main fun i=%d\n",i);
}
printf("Wait thread to finish…\n");
pthread_join(tid, &thread_result);//若不调用本函数,则当主线程结束时,子线程也结束
return 0;
}
验证性实验---------进程间通信
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid; //声明进程ID变量,其数据类型必须是 pid_t
pid = fork(); //复制创建进程,本函数在父进程、子进程中的返回值不同
if(pid == 0){ //子进程
int i =0;
for(i=0;i<5;i++){
printf("this is child process i=%d\n",i);
usleep(100);
}
}
if(pid>0){ //父进程
int i =0;
for(i=0;i<5;i++){
printf("this is parent process i=%d\n",i);
usleep(100);
}
}
return 0;
}
验证性实验----------管道间通信
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
int fd[2];
int ret;
char process_inter = 0;
ret = pipe(fd);
if (ret < 0) {
printf(“create pipe failure\n”);
return -1;
}
printf(“create pipe success\n”);
pid = fork();
if (pid == 0) { // child process code
int i = 0;
read(fd[0], &process_inter, 1); // if pipe empty sleep
while (process_inter == 0); // changed by parent process
for (i = 0; i < 5; i++) {
printf(“this is child process i=%d\n”, i);
usleep(100);
}
}
if (pid > 0) { // parent process code
int i = 0;
for (i = 0; i < 5; i++) {
printf(“this is parent process i=%d\n”, i);
usleep(100);
}
process_inter = 1;
sleep(5);
// change process_inter and write into pipe
write(fd[1], &process_inter, 1);
}
while (1);
return 0;
}
水果问题
#include “stdio.h”
#include “stdlib.h”
#include “pthread.h”
#include <unistd.h>
#include <time.h>
#include <pthread.h>
pthread_t son;
pthread_t dauther;
pthread_t father;
int apple=1;
int orang=0;
void *eat(){ //子线程
int j;
for (j = 0; j < 10; j++) {
usleep(100);
if(pthread_self()==son)
{
if(apple==0&&orang==1)
{
orang--;
printf("儿子吃了个橘子剩余的苹果数:%d,橘子数量:%d\n",apple,orang);
}
else
{
printf("儿子没吃东西 剩余的苹果数:%d,橘子数量:%d\n",apple,orang);
}
}
else if(pthread_self()==dauther)
{
if(apple==1&&orang==0)
{
apple--;
printf("女儿吃了一个苹果 剩余的苹果数:%d,橘子数量:%d\n",apple,orang);
}
else
{
printf("女儿没吃东西 剩余的苹果数:%d,橘子数量:%d\n",apple,orang);
}
}
else if(pthread_self()==father)
{
if(apple==0&&orang==0)
{
srand((unsigned)time(NULL));
if(rand()%2==1)
{
apple++;
printf("父亲将苹果数量+1 苹果数量:%d,橘子数量为:%d\n",apple,orang);
}
else
{
orang++;
printf("父亲将橘子+1 苹果数量:%d,橘子数量为:%d\n",apple,orang);
}
}
}
}
pthread_exit("Thank you for the CPU time");
}
int main(){ //主线程
int i;
//声明线程ID变量,数据类型必须是pthread_t
int ret1;
int ret2;
int ret3; //线程创建函数返回值
void *thread_result;
ret1 = pthread_create(&son, NULL, eat,NULL);
ret2 = pthread_create(&dauther,NULL,eat,NULL);
ret3 = pthread_create(&father, NULL, eat,NULL); //创建子线程
if(ret1<0){
printf(" create thread failure\n");
return -1;
}
if(ret2<0){
printf(" create thread failure\n");
return -1;
}
if(ret3<0){
printf(" create thread failure\n");
return -1;
}
printf("Wait thread to finish…\n");
pthread_join(father, &thread_result);
pthread_join(son, &thread_result);
pthread_join(dauther, &thread_result);//若不调用本函数,则当主线程结束时,子线程也结束
return 0;
}