Cod sursa(job #1982572)

Utilizator DEYDEY2Tudorica Andrei DEYDEY2 Data 19 mai 2017 14:19:26
Problema Traseu Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>


int main(int argc, char **argv)
{

char x[10];
char the_string[15] = {};
short size = 7;
int variant = 62307;
long number = 0;
short size_field = 0;
int length =0;
strcpy(x, "CONNECT");


	if (mkfifo("RESP_PIPE_62307", 0640) < 0 )
	{
		perror("Error creating the pipe");
		exit(1);	
	}


int fd1 = open("REQ_PIPE_62307", O_RDONLY);

	if (fd1 < 0)
	{
		perror("Error opening the request pipe");
		exit(1);
	}

int fd = open("RESP_PIPE_62307", O_WRONLY);

	if (fd < 0)
	{
		perror("Error opening the response pipe");
		exit(1);
	}

write(fd, &size, 1);
write(fd, x, sizeof(char)*size);

read(fd1, &size_field, 1);
printf("--size field : %d--\n ", size_field);
read(fd1, &the_string, size_field);
printf("-- our string :%s\n ", the_string);

length =4; 

printf("SUCCESS\n");

	while (strcmp(the_string,"EXIT") != 0)
	{
		if (strstr(the_string,"PING") != NULL) 
		{
			printf("PING HAS BEEN READ\n");
			write(fd, &length, 1);
			write(fd, "PING", size_field);	
			write(fd, &length, 1);
			write(fd, "PONG", size_field);	
			write(fd, &variant, sizeof(int));
				
		}

		if (strstr(the_string,"CREATE_SHM") != NULL) 
		{
			printf("CREATE SHM HAS BEEN READ\n");
			read(fd1, &number, sizeof(long));
			printf("the number read is shm is  : %ld\n", number);
			int shmId;
			shmId = shmget(17293, number, IPC_CREAT|0664);
			printf("the shm id = %d", shmId);
			if (shmId < 0) 
			{
				write(fd, &size_field, 1);
				write(fd, "CREATE_SHM", size_field);
				size_field = 5;
				write(fd, &size_field, 1);
				write(fd, "ERROR", size_field);
				size_field = 10;	
			}
			else 
			{
				int *pInt = (int*) shmat(shmId, 0, 0);

				if (*pInt == -1) 
				{
					perror("Error attaching shared memory area");
					exit(1);
				}
				write(fd, &size_field, 1);
				write(fd, "CREATE_SHM", size_field);
				size_field = 7;
				write(fd, &size_field, 1);
				write(fd, "SUCCESS", size_field);
				size_field= 10;
			}
	
			
	
		}

		read(fd1, &size_field, 1);
		read(fd1, &the_string, size_field);
	
	}

close(fd1);
close(fd);
	

return 0;
}