hello i 39 m completly stuck and i have spend soo mch time on this i provided the co 5192145
Hello, I'm completly stuck and i have spend soo mch time on this I provided the code that i have so far.
Thank you
THE PURPOSE OF THIS HOMWORK WAS TO IMPLEMINT IPCs:
message queues, semaphores, and shared memory. The sources given are:
ftok( ) – generates and retrieves unique key
IPC |
Create & Attach |
Command & Control |
Operations |
|
msg |
msgget(…) |
msgctl(…) |
msgsnd(…) |
msgrcv(…) |
sem |
semget(…) |
semctl(…) |
semop(…) |
|
shm |
shmget(…) |
shmctl(…) |
shmat(…) |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct mymesg {
// defined by the program
long msgtype; // a positive number assigned by the programs
char msgtext[???]; // message up to ??? bytes fixed
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
this exercise has 2 parts. In each case you are to write a server to initialize the IPC structure, and then fork a client (which you also write) to process the structure created.
1) Write a server and client for message queues. The server should enqueue several messages (the message data can be a string of characters), and the client should dequeue it and write the message data read to an output file.
2) Write a server and client for shared memory. The server should deposit some (character) data into shared memory and the client should read what is there, and write the results to an output file.
Resouces avaialable:
// Server
#include
#include
#include
#include
#include
#include
#include
#include
extern int errno;
int main (int argc, char *argv[]) {
key_t key;
int s_id;
union semun semset;
int val;
////////////////////////////////////////////////////////////////////////////////////////
FILE * pFile;
pFile = fopen(sem.txt,”a”) // create file
fprintf(pfile, “n what you want to write to the file”); // writing into the file.
fclose(pfile); // closing the file
////////////////////////////////////////////////////////////////////////////////////////
/* this assumes a file by the name of sem has been created in your CWD
key = ftok(“./sem”, 1234);
printf(“server: key = 0x%xn”, key);
/* the server creates a semaphore array with 1 semaphore in the array
if ((s_id = semget(key, 1, IPC_CREAT | 0644))
perror(“server: semget”);
exit(1);
}
printf(“server: sem id = 0x%xn”, s_id);
/* this is for illustration – it gets the default value of the semaphore
after it has been created */
if ((val = semctl(s_id, 0, GETVAL, semset))
perror(“server: semctl”);
exit(1);
}
printf(“server: sem val currently set at %dn”, val);
/* this sets the semaphore value to 1 (indicating it is available) */
semset.val = 1;
if (semctl(s_id, 0, SETVAL, semset)
perror(“server: semctl”);
exit(1);
}
printf(“server: sem val successfully set to 1n”);
/* this obtains the semaphore, making it temporarily not available for
other processes */
semoparr.sem_num = 0;
semoparr.sem_flg = SEM_UNDO;
semoparr.sem_op = -1;
if (semop(s_id, &semoparr, 1) != 0) {
perror(“server: semop”);
exit(1);
}
printf(“server: semop successfully set to 0n”);
/* for illustration – delay 10 seconds holding the emaphore so the
client will block trying to obtain it during this interval */
sleep(10);
/* this releases the semaphore, making it available for other processes */
semoparr.sem_op = 1;
if (semop(s_id, &semoparr, 1) != 0) {
perror(“server: semop”);
exit(1);
}
printf(“server: semop successfully set to 1n”);
exit(0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Client
#include
#include
#include
#include
#include
#include
#include
extern int errno;
int main (int argc, char *argv[]) {
key_t key;
int s_id;
struct sembuf semoparr;
union semun semset;
int val;
key = ftok(“./sem”, 1234);
printf(“client: key = 0x%xn”, key);
if ((s_id = semget(key, 0, 0))
perror(“client: semget”);
exit(1);
}
printf(“client: sem id = 0x%xn”, s_id);
if ((val = semctl(s_id, 0, GETVAL, semset))
perror(“client: semctl”);
exit(1);
}
printf(“client: sem val currently set at %dn”, val);
/*
semset.val = 1;
if (semctl(s_id, 0, SETVAL, semset)
perror(“client: semctl”);
exit(1);
}
printf(“client: sem val successfully set to 1n”;
*/
semoparr.sem_num = 0;
semoparr.sem_flg = SEM_UNDO;
semoparr.sem_op = -1;
if (semop(s_id, &semoparr, 1) != 0) {
perror(“client: semop”);
exit(1);
}
printf(“client: semop successfully set to 0n”);
semoparr.sem_op = 1;
if (semop(s_id, &semoparr, 1) != 0) {
perror(“client: semop”);
exit(1);
}
printf(“client: semop successfully set to 1n”);
exit(0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////