Здравейте! Опитвам се да съставя програма, която създава два процеса: единият чете, другият извежда. Проблемът е, че увисва още при започване. Ето го кодът, ако някой може да ми каже какъв е проблема:
GeSHi (C):
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct point {
int ID;
char* text;
int lock;
int readed;
}*p;
extern getmem(int memory);
extern testandset(int *lock);
char *messages[] = {
"Segmantation Fault",
"Unhandled Exception",
"Stack Overflow",
"Core Dump",
"General Protection Fault",
"Page Fault",
"Storage Violation",
"Bounds Check Fault",
"Null Pointer Exception",
"Divide Fault",
"NMI Interrupt",
"Overflow Trap",
"Invalid Opcode Fault",
"Runtime Error",
"Unspecified Fatal Error",
"Nuff said",
"Invalid Task State Segment Fault",
"Not Present Fault",
"Access Violation",
"Invalid Option",
"" /* The last message will never got read, so put it empty string is used */
};
int main(void) {
int pid, id=-1;
p = (struct point *)getmem(666);
p->readed = 1;
p->lock = 0;
p->ID = 0;
switch(fork()) {
case -1: /* Error */
printf("Fork error! Exiting...\n");
return 1;
break;
case 0: /* Child - Reading Messages */
while(messages[id+1]!=NULL) {
while (testandset(&(p->lock))) {}
if(p->ID!=id) {
if(id==-1) sleep(3);
else sleep(1);
id = p->ID;
}
p->readed = 1;
p->lock = 0;
}
printf("\n\tEnd of child process\n");
break;
default: /* Parrent - Generate Messages */
while(messages[p->ID]!=NULL) {
while(p->readed==0) {}
while (testandset(&(p->lock))) {}
p->text = messages[p->ID];
p->ID++;
p->readed = 0;
p->lock = 0;
}
wait();
printf("\n End of parent process\n");
}
return 0;
}