Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

C program to create a binary ppm file using parallel execution

Assignment 4
Rewrite your solution to Assignment 2 with changed requirements on parallel execution.
Requirement from previous assignment:
• The parent process waits for the child process to terminate before creating the next.
Replacement:
• The parent process will not wait for the child process to terminate before creating the
next process.
• The processes will do the calculation in parallel. Add some sleep time to simulate larger
calculation.
• The processes will write the data into the image file sequentially. This is coordinated by
the parent using signals.
• You can only use signal for inter-process communication.

Answer:

/*
Title: Assignment #4 
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
void handler(int dummy){ };
char color[5][3];
int main (int argc, char *argv[]){

	const int dimx = 1000, dimy = 1000;
	char buffer[]="P6\n1000 1000\n255\n";
	int pid1,pid2,pid3,pid4,pid5,pid6,pid7,pid8,pid9,pid10;
	int row,col,space,n,fd,status;
	//accepting input arguments
	for(int i=2;i<7;i++){
		if(strcmp(argv[i],"red")==0){
			color[i][0]= 255;color[i][1]= 0;color[i][2]= 0;
		}
		if(strcmp(argv[i],"green")==0){
			color[i][0]= 0;color[i][1]= 255;color[i][2]= 0;
		}
		if(strcmp(argv[i],"blue")==0){
			color[i][0]= 0;color[i][1]= 0;color[i][2]= 255;
		}
		if(strcmp(argv[i],"yellow")==0){
			color[i][0]= 255;color[i][1]= 255;color[i][2]= 0;
		}
		if(strcmp(argv[i],"orange")==0){
			 color[i][0]= 255;color[i][1]= 165;color[i][2]= 0;
		}
		if(strcmp(argv[i],"cyan")==0){
			color[i][0]= 0;color[i][1]= 255;color[i][2]= 255;
		}
		if(strcmp(argv[i],"magenta")==0){
			color[i][0]= 255;color[i][1]= 0;color[i][2]= 255;
		}
		if(strcmp(argv[i],"ocean")==0){
			color[i][0]= 43;color[i][1]= 101;color[i][2]= 236;
		}
		if(strcmp(argv[i],"violet")==0){
			 color[i][0]= 138;color[i][1]= 43;color[i][2]= 226;
		}
	}

    fd = open(argv[1], O_CREAT|O_WRONLY|O_TRUNC, 0755);
    write(fd,&buffer , 17);

	pid1=fork();
	if(pid1<0){
		printf("error");
	}
	else if(pid1==0){//child
		signal(SIGALRM, handler);  // receives signal from parent
		pause();// pause until recieve signal
		for(n=0;n<dimy/10;n++){
			for(space=0;space<(dimx/2);space++){
				write(fd, &color[3], 3);
			}
			for(space=0;space<dimx/2;space++){
				write(fd, &color[4], 3);
			}
		}
	}

	else{
		sleep(1);  // parents sleeps for 1 sec, letting child to reach pause
		kill(pid1,SIGALRM);
		sleep(10);
		exit(0);
	}

    pid2=fork();
	if(pid2<0){
		printf("error");
	}
	else if(pid2==0){
		signal(SIGALRM, handler);
		pause();
		//sleep(1);
		for(n=0;n<dimy/10;n++){
			for(space=0;space<(dimx/2);space++){
				write(fd, &color[3], 3);
			}
			for(space=0;space<dimx/2;space++){
				write(fd, &color[4], 3);
			}
		}
	}

	else{
		sleep(2);
		kill(pid2,SIGALRM);
		sleep(9);
		exit(0);
	}

	pid3=fork();
	if(pid3<0){
		printf("error");
	}
	else if(pid3==0){
		signal(SIGALRM, handler);
		pause();
		for(n=0;n<dimy/20;n++){
			for(space=0;space<(dimx/2);space++){
				write(fd, &color[3], 3);
			}
			for(space=0;space<dimx/2;space++){
				write(fd, &color[4], 3);
			}
		}
		for (row=0;row<(dimy/20);row++){
			for(space=0;space<dimx/4;space++){
				write(fd, &color[3], 3);
			}
			for(space=0;space<((dimx/4)-row);space++){
				write(fd, &color[3], 3);
			}
			for(col=0;col<(2*row);col++){
				write(fd, &color[2], 3);
			}
			for(space=0;space<((dimx/4)-row);space++){
				write(fd, &color[4], 3);
			}
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[4], 3);
			}
		}
	}
	else{
		sleep(3);
		kill(pid3,SIGALRM);   // send signal to child to do calculations
		sleep(8);
		exit(0);
	}

	pid4=fork();
	if(pid4<0){
		printf("error");
	}
	else if(pid4==0){
		signal(SIGALRM, handler);
		pause();
		for (row=0;row<(dimy/10);row++){
			for(space=0;space<dimx/4;space++){
				write(fd, &color[3], 3);
			}
			for(space=0;space<((dimx/5)-(row));space++){
				write(fd, &color[3], 3);
			}
			for(col=0;col<(100+(2*row));col++){
				write(fd, &color[2], 3);
			}
			for(space=0;space<((dimx/5)-(row));space++){
				write(fd, &color[4], 3);
			}
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[4], 3);
			}
		}
	}
	else{
		sleep(4);
		kill(pid4,SIGALRM);
		sleep(7);
		exit(0);
	}

	pid5=fork();
	if(pid5<0){
		printf("error");
	}
	else if(pid5==0){
		signal(SIGALRM, handler);
		pause();
		for (row=0;row<(dimy/10);row++){
			for(space=0;space<dimx/4;space++){
				write(fd, &color[3], 3);
			}
			for(space=0;space<((dimx/10)-row);space++){
				write(fd, &color[3], 3);
			}
			for(col=0;col<(300+(2*row));col++){
				write(fd, &color[2], 3);
			}
			for(space=0;space<((dimx/10)-row);space++){
				write(fd, &color[4], 3);
			}
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[4], 3);
			}
		}
	}
	else{
		sleep(1);
		kill(pid5,SIGALRM);
		sleep(6);
		exit(0);
	}

	pid6=fork();
	if(pid6<0){
		printf("error");
	}
	else if(pid6==0){
		signal(SIGALRM, handler);
		pause();
		for (row=(dimy/10);row>0;row--){
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[5], 3);
			}
			for(space=0;space<((dimx/10)-row);space++){
				write(fd, &color[5], 3);
			}
			for(col=0;col<(300+(2*row));col++){
				write(fd, &color[2], 3);
			}
			for(space=0;space<((dimx/10)-row);space++){
				write(fd, &color[6], 3);
			}
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[6], 3);
			}
		}
	}
	else{
		sleep(1);
		kill(pid6,SIGALRM);
		sleep(5);
		exit(0);
	}

	pid7=fork();
	if(pid7<0){
		printf("error");
	}
	else if(pid7==0){
		signal(SIGALRM, handler);
		pause();
		for (row=(dimy/10);row>0;row--){
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[5], 3);
			}
			for(space=0;space<((dimx/5)-(row));space++){
				write(fd, &color[5], 3);
			}
			for(col=0;col<(100+(2*row));col++){
				write(fd, &color[2], 3);
			}
			for(space=0;space<((dimx/5)-(row));space++){
				write(fd, &color[6], 3);
			}
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[6], 3);
			}
		}
	}
	else{
		sleep(1);
		kill(pid7,SIGALRM);
		sleep(4);
		exit(0);
	}

	pid8=fork();
	if(pid8<0){
		printf("error");
	}
	else if(pid8==0){
		signal(SIGALRM, handler);
		pause();
		for (row=(dimy/20);row>0;row--){
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[5], 3);
			}
			for(space=0;space<((dimx/4)-(row));space++){
				write(fd, &color[5], 3);
			}
			for(col=0;col<((2*row));col++){
				write(fd, &color[2], 3);
			}
			for(space=0;space<((dimx/4)-(row));space++){
				write(fd, &color[6], 3);
			}
			for(space=0;space<(dimx/4);space++){
				write(fd, &color[6], 3);
			}
		}
		for(n=0;n<(dimy/20);n++){

			for(space=0;space<(dimx/2);space++){
				write(fd, &color[5], 3);
			}
			for(space=0;space<(dimx/2);space++){
				write(fd, &color[6], 3);
			}
		}
	}
	else{
		sleep(1);
		kill(pid8,SIGALRM);
		sleep(4);
		exit(0);
	}

	pid9=fork();
	if(pid9<0){
		printf("error");
	}
	else if(pid9==0){
		signal(SIGALRM, handler);
		pause();
		for(n=0;n<(dimy/10);n++){

			for(space=0;space<(dimx/2);space++){
				write(fd, &color[5], 3);
			}
			for(space=0;space<(dimx/2);space++){
				write(fd, &color[6], 3);
			}
		}
	}
	else{
		sleep(1);
		kill(pid9,SIGALRM);
		sleep(3);
		exit(0);
	}

	pid10=fork();
	if(pid10<0){
		printf("error");
	}
	else if(pid10==0){
		signal(SIGALRM, handler);
		pause();
		for(n=0;n<(dimy/10);n++){

			for(space=0;space<(dimx/2);space++){
				write(fd, &color[5], 3);
			}
			for(space=0;space<(dimx/2);space++){
				write(fd, &color[6], 3);
			}
		}
	}
	else{
		sleep(1);
		kill(pid10,SIGALRM);
		exit(0);
	}
	return 0;

}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!