操作系统实验报告一
一父子进程之间的同步之例
1代码
#include
main( )
{
int pid1;
if(pid1=fork()) /*create child1 用正确完成时,给父进程返回地是被创建子进程的标识,给子进程返回的是0;创建失败时,返回给父进程的时-1;*/
{ if (fork()) /*create the child2*/
{printf (“parent’s context.\n”);
printf(“parent is waiting the child1 terminate.\n);
wait(0);
printf(“parent is waiting the child2 terminate.\n”);
wait(0);// 父进程同步等待子进程结束,即无子进程结束,父进程等待
printf(“parent terminate.\n”);
exit(0);//进程终止自己
}
else
/* child2*/
printf(“child2’s context.\n”);
sleep(5);//睡眠5毫秒
printf(“ child2 terminate.\n”);
exit(0);
}
else { if(pid1==0)/* child1 */
{ printf(“child1’s context.\n”);
sleep(10);//睡眠10毫秒
printf(“child1 terminate.\n”);
exit(0);//终止退出本进程
}
}
}
3分析
由主进程调用两次fork()方法创建了两个子进程,主进程输出parent’s context parent is waiting the child1 terminate.之后释放资源等待子进程执行,子进程谁抢到cpu控制权是随机的。每一个子进程都是child’s context.之后随眠一段时间,在执行child terminate.最终等都输出结束,程序结束。
我的输出结果所反映的是主线程创建两个子进程,然后子进程2抢到执行权,由于睡眠了导致子进程 1抢到执行权。然后子进程1睡眠了继续执行直到结束。然后主进程又继续执行输出,释放资源,子进程2抢到,输出,结束,主进程输出结束。二管道通信机制
1代码
#include
#include
#include
int pipe( int filedes[2]);
char parent[]=”a message to pipe’ communication.\n”;
main()
{ int pid,chan1[2];
char buf[100];//缓冲字符数组
pipe(chan1);//创建管道 chan1[0]为读,chan[1]为写
pid=fork();//创建进程
if(pid<0)
{ printf(“to create child error\n”);//提示创建创建进程失败
exit(1);
}
if(pid>0)
{ close(chan1[0]); /*父进程关闭读通道*/
printf(“parent process sends a message to child.\n”);
write(chan1[1],parent,sizeof(parent));//把parent的内容写到chan1[1]中
close(chan1[1]);
printf(“parent process waits the child to terminate.\n”);
wait(0);
printf(“parent process terminates.\n”);
}
else{
close(chan1[1]);/*子进程关闭写通道*/
read(chan1[0],buf,100);//chan1中的内容被读到buf缓冲区中
printf(“the message read by child process form parent is %s.\n”,buf);
close (chan1[0]);//关闭读通道
printf(“child process terminates\n”);
}
}
2运行截图
截图丢失
3分析
通过使用管道实现两个和多个进程之间的通信。所谓管道,就是将一个进程的标准输出与另一个进程的标准输入联系在一起,进行通信的一种方法。同组进程之间可用无名管道进行通信,不同组进程可通过有名管道通信。
三Linux中的多线程编程threads.c
1代码
#include
#include
#include
#include
#define MAX 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0, i;
void *thread1()
{
printf ("thread1 : I'm thread 1\n");
for (i = 0; i < MAX; i++)
{
printf("thread1 : number = %d\n",number);
pthread_mutex_lock(&mut);//上锁
number++;
pthread_mutex_unlock(&mut);//释放锁
sleep(2);
}
printf("thread1 :主函数在等我完成任务吗?\n");
pthread_exit(NULL);//退出
}
void *thread2()
{
printf("thread2 : I'm thread 2\n");
for (i = 0; i < MAX; i++)
{
printf("thread2 : number = %d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("thread2 :主函数在等我完成任务吗?\n");
pthread_exit(NULL);
}
void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread)); //comment1
/*创建线程*/
if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
printf("线程1创建失败!\n");
else
printf("线程1被创建\n");
if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
//comment3
printf("线程2创建失败");
else
printf("线程2被创建\n");
}
void thread_wait(void){
/*等待线程结束*/
if(thread[0] !=0) { //comment4
pthread_join(thread[0],NULL);//等待一个线程的结束
printf("线程1已经结束\n");
}
if(thread[1] !=0) { //comment5
pthread_join(thread[1],NULL);//等待一个线程的结束
printf("线程2已经结束\n");
}
}
int main()
{
/*用默认属性初始化互斥锁*/
pthread_mutex_init(&mut,NULL);//初始化操作
printf("我是主函数哦,我正在创建线程,呵呵\n");
thread_create();//调用创建线程函数
printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");
thread_wait();调用 thread_wait函数
return 0;
}
2运行截图
3分析
主线程创建之后调用创建子线程的函数,在调用wait函数,保证每个子线程输出一个number之后释放执行权。自己和另一个线程都有可能抢夺导致行权。number作为两个线程的共享资源可能导致同步问题,解决的方法就是通过调用
pthread_mutex_lock和pthread_mutex_unlock来给进程加锁。