Operating system – process control

Appendix 1. Process creation #include#includemain(){ int p1, p2; if(p1=fork()) /*The parent process was created successfully*/ putchar('b'); else {if(p2=fork()) /*The parent process was created successfully*/ putchar('c'); else putchar('a'); /*Sub-process execution*/ })2. The mutual exclusion of processes 1>Synchronization #include#includemain(){ int p1,p2,i ; if(p1=fork()){ for(i=0;i<500;i++) printf("parent%d
",i); wait(0); /* Ensure that the father The process will not terminate*/ exit(0); /* End the process*/} else {if(p2=fork()) {for(i=0;i<500;i++) printf("son %d
 ",i); wait(0); /* Ensure that the parent process will not terminate before the child process terminates*/ exit(0); /* Signal 0 to the parent process and the process exits*/} else {for(i =0;i<500;i++) printf("grandchild %d
",i); exit(0);} }}2>mutual exclusion#include#includemain( ){ int p1,p2,i; if(p1=fork()) {lockf(1,1,0);/* lock the parent process to achieve mutual exclusion*/ for(i=0;i<500 ;i++) printf("parent %d
",i); lockf(1,0,0);/*Unlock the parent process*/ wait(0); /* Ensure that the parent process is terminated before the child process is terminated Will not terminate*/ exit(0); /* End the process*/} else {if(p2=fork()) { lockf(1,1,0);/* lock the parent process to achieve mutual exclusion*/ for(i=0;i<500;i++) printf("son %d
",i); lockf( 1,0,0);/*Unlock the parent process*/ wait(0); /* Ensure that the parent process will not terminate before the child process terminates*/ exit(0);} else {lockf(1,1 ,0);/*Lock the child process to achieve mutual exclusion*/ for(i=0;i<500;i++) printf("grandchild %d
",i); lockf(1,0,0 );/*Unlock the child process*/ exit(0);} }} 3. Soft interrupt communication of the process #include#include#include void waiting( ),stop(),alarming();int wait_mark;main(){ int p1,p2; if(p1=fork()) /*create child process p1*/ {if(p2=fork()) /*create The child process p2*/ {wait_mark=1; signal(SIGINT,stop); /*The parent process receives the ^c signal, turn to stop*/ signal(SIGALRM, alarming); /* Accept SIGALRM, and call the alarming method waiting() ; while (wait_mark!=0); kill(p1,16); /*Send soft interrupt signal to p1 16*/ kill(p2,17); /*Send soft interrupt signal to p2 17*/ wait(0); /*Wait for child process p2 to achieve synchronization*/ wait(0); /*Wait for child process p1 to achieve synchronization*/ printf("parent process is killed!
"); exit(0);} else {wait_mark= 1; signal(17,stop); signal (SIGINT,SIG_IGN); /*The child process ignores the ^c signal*/ while (wait_mark!=0); lockf(1,1,0); /*Lock the child process to achieve mutual exclusion*/ printf("child process2 is killed by parent!
"); lockf(1,0,0); /*Unlock the child process*/ exit(0);}} else {wait_mark=1; signal(16,stop);signal( SIGINT,SIG_IGN); /*Ignore the ^c signal*/ while (wait_mark!=0) lockf(1,1,0); /*Lock the child process to achieve mutual exclusion*/ printf("child process1 is killed by parent!
"); lockf(1,0,0); /*Unlock the child process*/ exit(0); }}void waiting(){ sleep(5);if (wait_mark!=0) kill( getpid(),SIGALRM); /*Get the process number you want to interrupt, and return SIGALRM*/}void alarming(){ wait_mark=0;}void stop(){ wait_mark=0;}4. Pipeline communication of the process# include #include #include int pid1,pid2; main( ){ int fd[2]; /*Define the input and output for the pipeline*/char outpipe[ 100],inpipe[100];pipe(fd); /*create an unnamed pipe*/while ((pid1=fork( ))==-1); /*process creation failed*/if(pid1==0) {lockf(fd[1],1,0); /*fd[1] write side lock*/sprintf(outpipe,"child 1 process is sending message!"); /*Put data into the defined Buffer array In outpipe*/ write(fd[1],outpipe,50); /*Write a string of 50 bytes to the pipe*/sleep(5); /*Self-block for 5 seconds, let the displayed content sleep for 5 seconds Clock, that is, wait for 5 seconds before displaying. The child process pid1 and child process pid2 can also operate on the pipeline, because they belong to the same pipeline and share resources*/ lockf(fd[1],0,0); /*write end unlock*/ exit(0); }else {while((pid2=fork( ))==-1); if(pid2==0){ lockf(fd[1],1,0); /*fd[1] write side lock* / sprintf(outpipe,"child 2 process is sending message!"); write(fd[1],outpipe,50); sleep(5); lockf(fd[1],0,0); exit(0); } else {wait(0); /*Realize inter-process synchronization*/ read(fd[0],inpipe,50); /*Read 50 bytes of data from the fd[0] port to the buffer pointed to by inpipe */ printf("%s
",inpipe); wait(0); read(fd[0],inpipe,50); printf("%s
",inpipe); exit(0);}} }5. Creation, sending and receiving of inter-process messages #include  #include #include #include #define MSGKEY 75 /*Define keywords MEGKEY*/struct msgform /*message structure*/{ long mtype; char mtexe[100]; /*text length*/}msg;int msgqid,i;void CLIENT( ){ int i; msgqid= msgget(MSGKEY,0777|IPC_CREAT); /*Get the queue by keyword*/ for(i=10;i>=1;i--) {msg.mtype=i; printf("(client)s ent
"); msgsnd(msgqid,&msg,1030,0); /*Send the message msg into the msgid message queue, 1030 is the length of the message*/} exit(0);}void SERVER( ){ msgqid=msgget( MSGKEY,0777|IPC_CREAT); /*Create a message queue*/ do {msgrcv(msgqid,&msg,1030,0,0); /*Receive a message with a length of 1030 from the queue msgid msg*/ printf("(server)receive 
"); }while(msg.mtype!=1); /*When the message type is 1, release the queue*/ msgctl(msgqid, IPC_RMID,0); /*Delete msgqid's message queue*/}main() {if(fork()) {SERVER(); /*Create parent process*/ wait(0);} else CLIENT( ); /*Create child process*/} 6. Creation and attachment of process shared storage area And disconnection#include#include#include#define SHMKEY 75 /*define shared area keywords*/int shmid,i;int * addr; CLIENT(){ int i; shmid=shmget(SHMKEY,1024, 0777|IPC_CREAT); /*Get shared area, length 1024, keyword SHMKEY*/ addr=shmat(shmid,0,0); /*Get The starting address of the shared area addr*/ for(i=9;i>=0;i--) {while(*addr!= -1); printf("(client)sent
"); /*Print( client) sent*/ *addr= i; /* assign i to addr*/} exit(0);}SERVER(){ do {while(*addr = =-1); printf("(server)received
%d",*addr) ; /*Service process uses shared area*/ if(*addr!=0)*addr=-1;} while(*addr); wait(0); shmctl(shmid,IPC_RMID,0); /*Remove shared storage Area, return resources*/} main(){ shmid=shmget(SHMKEY,1024,0777|IPC_CREAT); /*Create a shared area*/ addr=shmat(shmid,0,0); /*The start address of the shared area is addr*/ *addr=-1; if(fork()) {SERVER();} else {CLIENT();} }7. Processor scheduling #include#include#include struct PCB *head,*tail; //Here is the global int global_time = 0; //Global time struct PCB{ char name[10]; //Process name struct PCB *next; //Next PCB pointer int run_time; //running time int priority; //priority char status; //current status};struct PCB* initPCB(struct PCB *p, int i){ p->name[0] ='P' ; p->name[1] = i + '0'; p->name[2] = 0; p->next = NULL; printf("Process %s
", p->name); printf( "input run_time: "); scanf("%d", &p->run_time); printf("input priori ty: "); scanf("%d", &p->priority); /*p->run_time = i+1; p->priority = i; p->status ='R';*/ return p; }//Enter the task name, time and priority, the running status is E void run(struct PCB *p){ printf("Process: %s, running @ tim %d, priority %d, run_time %d
", p->name, global_time, p->priority, p->run_time); global_time++; p->run_time--; if(p->priority> 0) p->priority--;) //output this task Status, global time plus 1, task still needs time minus 1, priority minus 1 void SortChain(struct PCB *phead,struct PCB *ptail){ struct PCB *pMax,*pMaxPre,*pPre,*pTemp; head = tail = NULL; //Initialize again, and re-add the arranged nodes to the linked list. pPre = pMax = pMaxPre = NULL; //pMax: the node with the highest priority, pMaxPre: pMax the previous node may use a doubly linked list Better if(phead == NULL){ head = tail = NULL; return; }else{ while(phead != NULL){ pMax = phead; pMaxPre = phead; for(pTemp = phead;pTemp != NULL; pTemp = pTemp->next){ if(pTemp->priority> pMax->priority ){ pMax = pTemp; pMaxPre = pPre;} pPre = pTemp;} if(pMax == phead){ phead = phead->next;} else{ pMaxPre->next = pMax->next;} pMax ->next = NULL; if (head == NULL){ tail = head = pMax; }else{ tail -> next = pMax; tail = pMax;}} })int main(void ){ int i; struct PCB *p = NULL, *q = NULL; //define pointers p and q, p is the PCB pointer of the queue to be run, and q is the PCB pointer of the queue to be inserted head = tail = NULL; for(i = 0;i <5;i ++){ p = (struct PCB *)malloc(sizeof(struct PCB)); //Allocate space, let p point to this PCB initPCB(p,i); p -> next = NULL; if(head == NULL){ // First link the process list Line it up! Not necessarily in the order of priority tail = head = p; }else{ tail -> next = p; tail = p;}} SortChain(head,tail); /// Arrange the nodes in priority order while(head != NULL) {//When the head node is not empty and there is PCB in the linked list printf("Current state:
"); for(p = head; p != NULL; p = p->next){ printf("Process %s, priority: %d, run_time %d
",p->name, p->priority, p->run_time);} //Let the p pointer go from head->next to the end, and output each one State p = head; run(p);//p points to the first PCB, and then schedules if(p->run_time <= 0){ printf("Process %s ends.
", p->name) ; p->status ='E'; ///free(p); ///No release here head = p-> next;} //If the PCB is over, output information, release space, proceed to the next Round loop SortChain(head,tail); //Arrange the nodes in priority order} //If you still need to run, insert the PCB into the appropriate position system("pause"); return 0;}

< p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5383 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.