Ok, guys I know that I'm asking too much questions, it's just that I really want to understand how things work. So I have a little project:
Make a program in C which runs three processes (A,B,C), which use a common memory (buffer). Process A generates 50 random numbers and writes then into the buffer. Process B writes down every even number into file F1. Process C writes down every odd number into file F2.
So far I have this:

#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <time.h>
int ID;
int *buffer;
int SemId;

void SemGet(int n)
{ 
SemId = semget(IPC_PRIVATE, n, 0600);
if (SemId == -1) {
printf("error!\n");
exit(1);
}
}
int SemSetVal(int SemNum, int SemVal)
{ 
return semctl(SemId, SemNum, SETVAL, &SemVal);
}
int SemOp(int SemNum, int SemOp)
{ 
struct sembuf SemBuf;
SemBuf.sem_num = SemNum;
SemBuf.sem_op = SemOp;
SemBuf.sem_flg = 0;
return semop(SemId, & SemBuf, 1);
}
void SemRemove(void)
{ 
(void) semctl(SemId, 0, IPC_RMID, 0);
}



void erase()
{

(void) shmdt((char *) buffer);
(void) shmctl(ID, IPC_RMID, NULL);
exit(0);
}

void process_A();
void process_B();
void process_C();

int main()
{

ID = shmget(IPC_PRIVATE,sizeof(int)*100,0600);
buffer = shmat(ID,NULL,0);
SemGet(2);
SemSetVal(0,0);
SemSetVal(1,0);

int i = 0;

if(fork()==0){
process_A();
}
if(fork()==0){
process_B();
}
if(fork()==0){
process_C();
}
wait(NULL);
wait(NULL);
wait(NULL);

erase();
SemRemove();

}


void process_A(){
srand((unsigned)(time(NULL)));
int i = 0;
for(i = 0; i < 50; i++){
buffer[i] = rand();
SemOp(0,1);
SemOp(1,1);
}
SemOp(0,1);
SemOp(1,1);
exit(0);
}
void process_B(){
FILE *F1 = fopen("F1.txt","w");
int i = 0;
while(i < 50){	
SemOp(0,-1);	
if(buffer[i]%2 == 1)
fprintf(F1,"%d: %d\n",i,buffer[i]);
i++;	

}

fclose(F1);	
exit(0);

}
void process_C(){

FILE *F2 = fopen("F2.txt","w");
int i = 0;
while(i < 50){
SemOp(1,-1);	
if(buffer[i]%2 == 0)
fprintf(F2,"%d: %d\n",i,buffer[i]);
i++;
}
fclose(F2);
exit(0);
}

And the problem is:
It says:

In function 'main'
on line 49:
'fork' undeclared (first use in this function)
(Each undeclared identifier is reported only once for each function it appears in)

So I've tried somehow to "declare" it, but no sucess by now.
Any ideas?

fork() requires the header file <unistd.h>

Oh yes. Thank you.
Well it does compile with no errors but I can't see F1.txt or F2.txt.
Any ideas what's the problem with my code?

When you create a file check the file handler to see if it is not null.
Also I dont like the fact that you have forked thrice in the main program. When you call fork() you create an additional process.So after the first call to fork is executed there will be 2 process and each of them will call a fork giving rise to 5 processes. I dont think this was what you had in mind

Probably you should look at threads.

Ok everyone, so I've come with another code, And the question is: What should I write in getmem()?

#include<fcntl.h>
#include<wait.h>
#include<stdio.h>
#include<iostream>
using namespace std;

extern char* getmem(int);

int main(){
	int ID=0, nID, status=0;
	int i;
	short *a=(short*)getmem(????);
	FILE *f;
	srand((unsigned)time(NULL));
	// Defining the processes
	cout<<"Starting process A...\n";
	ID++;
	nID=fork();
	if(nID<0){
		cout<<"Error!\n";
		return 0;
	}
	else if(nID!=0){
		wait(&status);
		cout<<"Starting process B...\n";
		ID++;
		nID=fork();
		if(nID<0){
			cout<<"Error!\n";
			return 0;
		}
		else if(nID!=0){
			cout<<"Starting process C...\n";
			ID++;
			nID=fork();
			if(nID<0){
				cout<<"Error!\n";
				return 0;
			}
			else if(nID!=0)
				ID=0;
		}
	}
	// The processes
	if(ID==1)
		for(i=0; i<50; i++){
			*(a+i)=rand();
		}
	if(ID==2){
		f=fopen("f1.txt", "w");
		if(f==NULL)
			cout<<"Error creating file!\n";
		for(i=0; i<50; i++){
			if((*(a+i)%2)==0)
				fprintf(f, "%i\n", *(a+i));
		}
		fclose(f);
	}
	if(ID==3){
		f=fopen("f2.txt", "w");
		if(f==NULL)
			cout<<"Error creating file!\n";
		for(i=0; i<50; i++){
			if((*(a+i)%2)!=0)
				fprintf(f, "%i\n", *(a+i));
		}
		fclose(f);
	}
	wait(&status);
	return 0;
}

And sorry guys but which libraries I should include for srand(), cout, fork() and rand()? - It gets me errors, it says that those functions are not declared.

And sorry guys but which libraries I should include for srand(), cout, fork() and rand()? - It gets me errors, it says that those functions are not declared.

This is not a C programming source any longer; it has metamorphosed into something else. There's a dedicated forum for such creature. Look for C++. Post there.

Ok thanks, I'll go to C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.