GIVEN CODE- FILL IN THE BLANK!
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
// Function ptototypes
int readX();
void writeX(int);
int main() /// chi read x ---> divi ---> write x into file
---> par read x --> sub--> write x into file---> chi
read x-->etc
{
int pid;
// pid: used to keep track of the child
process
int x = 19530; // x:
our value as integer
int i;
// iterator
// output inital value to the screen
printf("x = %d\n", x); /// x= 19530
// Write x to file
______;
// loop 5 times
for( i = 0; i<5; i++)
{
// output the current loop
iteration
printf("\nITERATION %d\n",
i+1);
// flush here so that the stdout
buffer doesn't
//
get printed in both processes
fflush(stdout);
// fork child process
if (________) ///
fork()
{
perror("Error
with fork");
exit(1);
}
// PARENT waits a second for the
child to go first
else if (_____)
{
_____;
}
// Both processes read x from
file
______;
// CHILD prints child, and
performs subraction
if (____)
{
printf("Child:
");
____; /// x= x -
5
}
// PARENT prints parent, and
performs division
else if (____)
{
printf("Parent:
");
____;
}
// Both processes output x value
and write x to file...
printf("x = %d\n", x);
______;
// The child terminates
if (____)
{
____;
}
}
exit(0);
}
/// Returns the value read from .shareX.dat /////// int
num=readX(); value in file---> num
int readX()
{
char xChar[10]; //
xChar: our value as a char
int fd;
// fd: file descriptor
// open to read and store x
if ( (fd = open(".shareX.dat", O_RDONLY )) == -1
)
{
perror("Error opening file");
exit(2);
}
// read xChar from the file
if ( read(fd, xChar, 10) == -1 )
{
perror("Error reading file");
exit(3);
}
// close file for reading
close(fd);
// convert xChar to int and return
return atoi(xChar);
}
/// Writes the writeX value to the file .shareX.dat ///////
writeX(num) ===> write num into file
void writeX(int writeX)
{
char xChar[10]; //
xChar: our value as a char
int fd;
// fd: file descriptor
int xBytes;
// how much to write
// open, clear, and create file if not
createdi
if ( (fd = open(".shareX.dat",
O_CREAT | O_TRUNC | O_WRONLY,
0644 )) == -1 )
{
perror("Error opening file");
exit(4);
}
// convert x to char
xBytes = sprintf(xChar, "%d", writeX);
// put xChar in file
if ( write(fd, xChar, xBytes) == -1 )
{
perror("Error writing to
file");
exit(5);
}
// close the file for writing
close(fd);
return;
}
program code to copy
main.c
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
// Function ptototypes
int readX();
void writeX(int);
int main()
{
int pid; // pid: used to keep track of the child process
int x = 19530; // x: our value as integer
int i; // iterator
// Write x to file
writeX(x);
// output inital value to the screen
printf("x = %d\n", x);
// loop 5 times
for( i = 0; i<5; i++)
{
// output the current loop iteration
printf("\nITERATION %d\n", i+1);
// flush here so that the stdout buffer doesn't
// get printed in both processes
fflush(stdout);
// Check if fork child process creates a process
if ((pid = fork()) == -1)
{
perror("Error with fork");
exit(1);
}
// Parent waits a second for the parent to go first
else if (pid > 0)
{
wait(0);
}
// Both processes read x from file
x = readX();
// Parent prints parent, and performs subraction
if (pid > 0)
{
x = x / 5;
printf("Parent: ");
}
// Child prints child, and performs division
else if (pid == 0)
{
x = x - 5;
printf("Child: ");
}
// Both processes output x value and write x to file...
printf("x = %d\n", x);
writeX(x);
// The child terminates
if (pid == 0)
{
exit(0);
}
}
exit(0);
}
/// Returns the value read from .shareX.dat
int readX()
{
char xChar[10]; // xChar: our value as a char
int fd; // fd: file descriptor
// open to read and store x
if ( (fd = open(".shareX.dat", O_RDONLY )) == -1 )
{
perror("Error opening file");
exit(2);
}
// read xChar from the file
if ( read(fd, xChar, 10) == -1 )
{
perror("Error reading file");
exit(3);
}
// close file for reading
close(fd);
// convert xChar to int and return
return atoi(xChar);
}
/// Writes the writeX value to the file .shareX.dat
void writeX(int writeX)
{
char xChar[10]; // xChar: our value as a char
int fd; // fd: file descriptor
int xBytes; // how much to write
// open, clear, and create file if not createdi
if ( (fd = open(".shareX.dat",
O_CREAT | O_TRUNC | O_WRONLY, 0644 )) == -1 )
{
perror("Error opening file");
exit(4);
}
// convert x to char
xBytes = sprintf(xChar, "%d", writeX);
// put xChar in file
if ( write(fd, xChar, xBytes) == -1 )
{
perror("Error writing to file");
exit(5);
}
// close the file for writing
close(fd);
return;
}
sample output
GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...
Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...
devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h> #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w'; if(argc...
GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/ // main function int main(int argc, char *argv[]) { // Variable declarations int fd; // file descripter char buffer[BUF_SIZE]; // string for...
I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){ int fd;...
Rewrite the program as a set of two programs that utilize a named pipe “ages” in the current folder for IPC and implement the same function as discussed above. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) { int fd[2], nbytes; pid_t childpid; int users = 95; char mystring[80]; char readbuffer[80]; char digit1,digit2; digit1 = (char) users%10; digit2 = (char ) users/10; mystring[0] = digit1; mystring[1] = digit2;...
Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...
What is the functionality of the following code? #include #include mainO <stdio.h> <unistd.h> int i,j; j-0: printf ("Ready to fork.n) i-fork; if 0) this code.\n"); printf "The child executes for (i-0; i?5; i++) printf("Child j-dn".j); else j-vaito printf("The parent executes this code. Ln" printf ("Parent j-dn",j);
#include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions int global_sum = 0; typedef struct{ char* word; char* filename; }MyStruct; void *count(void*str) { MyStruct *struc; struc = (MyStruct*)str; const char *myfile = struc->filename; FILE *f; int count=0, j; char buf[50], read[100]; // myfile[strlen(myfile)-1]='\0'; if(!(f=fopen(myfile,"rt"))){ printf("Wrong file name"); } else printf("File opened successfully\n"); for(j=0; fgets(read, 10, f)!=NULL; j++){ if (strcmp(read[j],struc->word)==0)...
Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...