//进程调度
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#define LEN sizeof(PCB)
#define NUM 8
struct PCB{
int name;
int runtime;
int runedtime;
int killtime;
struct PCB * next;
};
typedef struct PCB PCB;
int main(){
PCB *runqueue;
PCB *top,*tail,*temp;
int i;
for(i=0;i<NUM;i++){
temp=(PCB*)malloc(LEN);
temp->name=i;
temp->runtime=rand()%15;
temp->runedtime=0;
temp->next=NULL;
temp->killtime=0;
if(i==0){
top=temp;
tail=temp;
}else{
tail->next=temp;
tail=temp;
}
printf("process name %d,runtime=%d,runedtime=%d,killtime=%d\n",tail->name,tail->runtime,tail->runedtime,tail->killtime);
}
printf("*********************************************\n");
int timeslice=3;
while(top!=NULL){
runqueue=top;
top=top->next;
runqueue->next=NULL;
runqueue->runtime=runqueue->runtime-timeslice;
if(runqueue->runtime<=0){
runqueue->killtime=runqueue->runtime+timeslice;
runqueue->runedtime=runqueue->runedtime+runqueue->killtime;
runqueue->runtime=0;
printf("process name:%d,runtime=%2d,runedtime=%2d,killtime=%2d\n",runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime);
}
else{
runqueue->killtime=timeslice;
runqueue->runedtime=runqueue->runedtime+runqueue->killtime;
printf("process name:%d,runtime=%2d,runedtime=%2d,killtime=%2d\n",runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime);
tail->next=runqueue;
tail=tail->next;
}
}
}
//进程通信
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/msg.h>
#include <errno.h>
#define MAX_TEXT 1000
struct msg{
long int msgtype;
char text[MAX_TEXT];
};
int main(){
struct msg data;
int msgid=-1;
char buffer[BUFSIZ];
msgid=msgget((key_t)1234,0666|IPC_CREAT);
while(1){
printf("Enter:\n");
fgets(buffer,BUFSIZ,stdin);
strcpy(data.text,buffer);
data.msgtype=1;
msgsnd(msgid,(void*)&data,MAX_TEXT,0);
if(strncmp(buffer,"end",3)==0) break;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/msg.h>
#include <errno.h>
struct msg{
long int msgtype;
char text[BUFSIZ];
};
int main(){
struct msg data;
int msgid=-1;
long int msgtype=0;
msgid=msgget((key_t)1234,0666|IPC_CREAT);
while(1){
msgrcv(msgid,(void *)&data,BUFSIZ,msgtype,0);
printf("You wrote:%s",data.text);
if(strncmp(data.text,"end",3)==0) break;
}
msgctl(msgid,IPC_RMID,0);
}
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SHMKEY 1234
#define SHMSZ 1024
#define NUM 10
int main(){
int shmid;
int *addr;
int i;
shmid=shmget(SHMKEY,SHMSZ,0777);
addr=shmat(shmid,0,0);
for(i=0;i<NUM;i++){
while(*addr!=-1);
*addr=i;
printf("client sent:%d\n",*addr);
}
exit(0);
}
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SHMKEY 1234
#define SHMSZ 1024
#define NUM 10
int main(){
int shmid;
int *addr;
shmid=shmget(SHMKEY,SHMSZ,0777|IPC_CREAT);
addr=shmat(shmid,0,0);
do
{
*addr=-1;
while(*addr==-1);
printf("server received:%d\n",*addr);
}while(*addr!=NUM-1);
shmctl(shmid,IPC_RMID,0);
exit(0);
}
//1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/msg.h>
#include <errno.h>
#define MAX_TEXT 1000
struct msg_st
{
long int msg_type;
char text[MAX_TEXT];
};
int main()
{
struct msg_st data;
char buffer[BUFSIZ];
int msgid = -1;
// 建立消息队列
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
// 向消息队里中写消息,直到写入end
int i=8;
while (i)
{
printf("Enter some text: \n");
fgets(buffer, BUFSIZ, stdin);
data.msg_type = 1; // 注意2
strcpy(data.text, buffer);
// 向队列里发送数据
msgsnd(msgid, (void *)&data, MAX_TEXT, 0);
i--;
}
}
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SHMKEY 1234
#define SHMSZ 1024
#define NUM 10
int main(){
int shmid;
int *addr;
shmid=shmget(SHMKEY,SHMSZ,0777|IPC_CREAT);
addr=shmat(shmid,0,0);
do
{
*addr=-1;
while(*addr==-1);
printf("server received:%c\n",*addr);
}while(*addr!=111);
shmctl(shmid,IPC_RMID,0);
exit(0);
}
//2
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SHMKEY 1234
#define SHMSZ 1024
#define NUM 10
int main(){
int shmid;
int *addr;
int i;
shmid=shmget(SHMKEY,SHMSZ,0777);
addr=shmat(shmid,0,0);
for(i=97;i<112;i++){
while(*addr!=-1);
*addr=i;
i++;
printf("client sent:%c\n",*addr);
}
exit(0);
}
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SHMKEY 1234
#define SHMSZ 1024
#define NUM 10
int main(){
int shmid;
int *addr;
shmid=shmget(SHMKEY,SHMSZ,0777|IPC_CREAT);
addr=shmat(shmid,0,0);
do
{
*addr=-1;
while(*addr==-1);
printf("server received:%c\n",*addr);
}while(*addr!=111);
shmctl(shmid,IPC_RMID,0);
exit(0);
}
04-09
02-03
161