
实 验 报 告
教 师: 方敏
| 2016 年 12月 17日 | 
学 号: *****。。。
姓 名: xyz ---
实验地点: --- -------
实验时间: --------------------
实验一 创建进程
【实验软硬件环境】
Windows
【实验内容】
学会通过基本的Windows进程控制函数,由父进程创建子进程。
假设现在有这样一个工作,需要计算1--100的和,还需要做的一个工作是读写文件,我们可以让父进程计算,再创建一个子进程读写文件。
【实验原理】
首先由父进程创建子进程,让子进程创建一个文件并写入数据,子进程写文件过程中父进程继续执行计算操作,等子进程执行完以后,父进程读取文件内容输出,实现进程协同工作。
【实验程序及分析】
父进程:
#include #include int main(){     STARTUPINFO si;     PROCESS_INFORMATION pi;     ZeroMemory(&pi,sizeof(pi));     ZeroMemory(&si,sizeof(si));     si.cb=sizeof(STARTUPINFO);     if(CreateProcess("CHILD.exe",NULL,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)){         printf("子进程已创建!\\n");         int i,sum=0;         for(i=1;i<=100;++i){             sum+=i;             printf("sum=%d\\n",sum);         }         WaitForSingleObject(pi.hProcess,INFINITE);         FILE *fp;         fp=fopen("date.txt         char ch=fgetc(fp);         while(ch!=EOF){             putchar(ch);             ch=fgetc(fp);         }         fclose(fp);     }     else         printf("子进程创建失败!\\n");     return 0; } 子进程: #include #include int main(){     printf("子进程运行!\\n");     FILE *fp;     if(fp=fopen("date.txt         printf("已经创建文件!\\n");         int i;         for(i=48;i<58;i++) fputc(i,fp);         fputc('\\n',fp);         fclose(fp);         printf("已经写入数据:");          fp=fopen("date.txt         char ch=fgetc(fp);         while(ch!=EOF){             putchar(ch);             ch=fgetc(fp);         }         fclose(fp);     }     else printf("创建文件失败!\\n");     system("pause");     return 0; } 【实验结果截图】 实验二 线程共享进程数据 【实验软硬件环境】 Windows 【实验内容】 学习创建线程实现多工作同步运行,了解线程和进程之间的数据共享关系。 【实验原理】 在进程中定义全局共享数据,在线程中直接引用该数据进行更改并输出数据。 【实验程序及分析】 #include #include static int count; DWORD WINAPI ThreadProc(LPVOID IpParameter){     printf("新线程运行!\\n");     for(count=1;count<=5;count++){         printf("线程count=%d\\n",count);     }     printf("线程等待5秒钟!\\n");     Sleep(5000);     return 0; } int main(){     count=10;     printf("进程运行!\\n进程count=%d\\n",count);     HANDLE hEvent=CreateThread(NULL,0,ThreadProc,NULL,0,NULL);     WaitForSingleObject(hEvent,INFINITE);     CloseHandle(hEvent);     printf("新线程结束!\\n");     printf("进程结束!\\n");     return 0; } 【实验结果截图】 实验三 信号通信 【实验软硬件环境】 Windows 【实验内容】 父进程创建一个有名事件,由子进程发送事件信号,父进程获取事件信号后进行相应的处理。 【实验程序及分析】 父进程: #include #include int main(){     STARTUPINFO si;     PROCESS_INFORMATION pi;     ZeroMemory(&pi,sizeof(pi));     ZeroMemory(&si,sizeof(si));     si.cb=sizeof(STARTUPINFO);     if(!CreateProcess("ChildProcess.exe",NULL,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)){         printf("创建子进程失败!\\n");         return 0;     }     printf("Wait for event.\\n");     HANDLE hEvent=CreateEvent(NULL,FALSE,FALSE,"event");     if(WAIT_TIMEOUT==WaitForSingleObject(hEvent,10000)){         printf("等待事件信号超时!\\n");         return 0;     }     printf("Get the event.\\n");     CloseHandle(hEvent);     return 0; } 子进程: #include #include #include int main(){     HANDLE hEvent=OpenEvent(EVENT_ALL_ACCESS,TRUE,"event");     printf("Signal the event to Parent?[Y\\\\N]:");     char ch;     scanf("%c", &ch);     if(ch=='Y'){         SetEvent(hEvent);     }     else{        return 0;     }     Sleep(1000);     return 0; } 【实验结果截图】 实验四 匿名管道通信 【实验软硬件环境】 Linux 【实验内容】 学习使用匿名管道在两进程间建立通信。 【实验程序及分析】 #include #include #include #include #include int main(){  int chanel[2];int status=pipe(chanel);  if(status<0) printf("create pipe failed\\n");  char sendBuffer[]="pipe test!";char readBuffer[50];pid_t pid=fork();  if(pid>0){      close(chanel[0]);     if(write(chanel[1],sendBuffer,strlen(sendBuffer))>0)  printf("parent write success\\n");     close(chanel[1]);     printf("parent close pipe successed\\n");     wait(NULL);  }   else if(pid==0){    close(chanel[1]);    int num=read(chanel[0],readBuffer,30);    printf("read data from pipe successed\\n");    printf("the data: %s\\n",readBuffer);  }  else  printf("creat child process failed\\n");  return 0; } 【实验结果截图】 实验五 信号量实现进程同步 【实验软硬件环境】 Linux 【实验内容】 生产者进程生产产品,消费者进程消费产品。 当生产者进程生产产品时,如果没有空缓冲区可用,那么生产进程必须等待消费进程释放出一个缓冲区。 当消费者进程消费产品时,如果缓冲区没有产品,那么消费者进程将被阻塞,直到新的产品被生产出来。 【实验程序及分析】 #include #include  #include  #include  #include #include #include union semun{     int val;     struct semid_ds *buf;     unsigned short *array; }; int fullSEG,emptySEG,mutex; int semaphore_p(int semId); int semaphore_v(int semId); int setValue(int semId,int value); int i; int main(){      fullSEG = semget((key_t)1234, 1, 0666 | IPC_CREAT);     setValue(fullSEG, 0);      emptySEG = semget((key_t)1235, 1, 0666 | IPC_CREAT);     setValue(emptySEG, 6);      mutex = semget((key_t)1236, 1, 0666 | IPC_CREAT);     setValue(mutex, 1);     pid_t customerOne, customerTwo;      for(i = 0; i < 2; i++){         pid_t temp = fork();         if(temp == 0) {             if(i == 0) customerOne = getpid();             if(i == 1) customerTwo = getpid();             break;         }     }     if(getpid() == customerOne){         while(1){             sleep(3);             semaphore_p(fullSEG);             semaphore_p(mutex);             int value = semctl(fullSEG, 0, GETVAL, 0);             printf("Customer %d: use 1 thing, now there are %d things\\n", getpid(), value);             semaphore_v(mutex);             semaphore_v(emptySEG);         }     }     else if(getpid() == customerTwo) {         while(1){             sleep(3);             semaphore_p(fullSEG);             semaphore_p(mutex);             int value = semctl(fullSEG, 0, GETVAL, 0);             printf("Customer %d: use 1 thing, now there are %d things\\n", getpid(), value);             semaphore_v(mutex);             semaphore_v(emptySEG);         }     }     else{         while(1){             sleep(1);             semaphore_p(emptySEG);             semaphore_v(fullSEG);             semaphore_p(mutex);             int value = semctl(fullSEG, 0, GETVAL, 0);             printf("Producer %d: produce 1 thing, now there are %d things\\n", getpid(), value);             semaphore_v(mutex);         }     }     return 0;     } int semaphore_p(int semId){     struct sembuf sem_b;     sem_b.sem_num = 0;     sem_b.sem_op = -1;     sem_b.sem_flg = SEM_UNDO;     if(semop(semId, &sem_b, 1) == -1){         printf("semaphore_p failed\\n");     }     return 0; } int semaphore_v(int semId){     struct sembuf sem_b;     sem_b.sem_num = 0;     sem_b.sem_op = 1;     sem_b.sem_flg = SEM_UNDO;     if(semop(semId, &sem_b, 1) == -1)         printf("semaphore_v failed\\n");     return 0; } int setValue(int semId, int value){     union semun sem_union;     sem_union.val = value;     if(semctl(semId, 0, SETVAL, sem_union) == -1) printf("Setting %d value is error\\n", semId);     return 0; } 【实验结果截图】 实验六 共享主存实现进程通信 【实验软硬件环境】 Linux 【实验内容】 利用共享主存解决读写者问题。要求由写者创建一个共享主存,并向其中写入数据,读者进程从该共享主存中读取数据。 【实验程序及分析】 写者: #include #include #include #include #include struct people{  char name[4];  int age; }; void main(){   struct people *p;   int shmid=shmget((key_t)1234,sizeof(struct people),0666|IPC_CREAT);   p=shmat(shmid,0,0);   strcpy(p->name,"ppp");   p->age=17;   sleep(5);   return; } 读者: #include #include #include struct people{  char name[4];  int age; }; void main(){   struct people *p;   int shmid=shmget((key_t)1234,sizeof(struct people),0666|IPC_CREAT);   p=shmat(shmid,0,0);   printf("name:%s,age:%d\\n",p->name,p->age);   return; } 【实验结果截图】
