#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <string.h> #include<unistd.h> int main() {int menu = 0; printf("\n\n\n"); printf("************************ Code By ******************** \n"); printf(" avb\n"); printf("********************* WELCOME *********************** \n"); do { printf("\n\n"); printf("1. Create and write a File \n"); printf("2. Read from a File\n"); printf("3. Pipe\n"); printf("4. dup2\n"); printf("5. Fork\n"); printf("6. To exit from the program\n"); printf("Hello User please Enter your choice ="); scanf("%d", &menu); switch (menu) { case 1: { FILE *fptr; char name[20]; int age; float salary; fptr = fopen ("emp.rec", "w"); /*open for writing*/ if (fptr == NULL) { printf("File does not exists\n"); return; } printf("Enter the name\n"); scanf("%s", name); fprintf(fptr, "Name = %s\n", name); printf("Enter the age\n"); scanf("%d", &age); fprintf(fptr, "Age = %d\n", age); printf("Enter the salary\n"); scanf("%f", &salary); fprintf(fptr, "Salary = %.2f\n", salary); fclose(fptr); break; } case 2: { FILE * pFile; char ch, file_name [250]; pFile = fopen ("emp.rec" , "r"); if (pFile == NULL) perror ("Error opening file");else { printf("The contents of %s file are :\n", file_name); while( ( ch = fgetc(pFile) ) != EOF ) printf("%c",ch);fclose(pFile); } break; } case 3: { void write_to_pipe (int file) { FILE *stream; stream = fdopen (file, "w"); fprintf (stream, "hello, world!\n"); fprintf (stream, "goodbye, world!\n"); fclose (stream); } void read_from_pipe (int file) { FILE *stream; int c; stream = fdopen (file, "r"); while ((c = fgetc (stream)) != EOF) putchar (c); fclose (stream); } pid_t pid; int mypipe[2]; /* Create the pipe. */ if (pipe (mypipe)) { fprintf (stderr, "Pipe failed.\n"); // return EXIT_FAILURE; } /* Create the child process. */ pid = fork (); if (pid == (pid_t) 0) { /* This is the child process. Close other end first. */ close (mypipe[1]); read_from_pipe (mypipe[0]); // return EXIT_SUCCESS; } else if (pid < (pid_t) 0) { /* The fork failed. */ fprintf (stderr, "Fork failed.\n"); return EXIT_FAILURE; } else { /* This is the parent process. Close other end first. */ close (mypipe[0]); write_to_pipe (mypipe[1]); //return EXIT_SUCCESS;} break; }case 4 { //First, we're going to open a file int file = open("myfile.txt", O_APPEND | O_WRONLY); if(file < 0) return 1;//Now we redirect standard output to the file using dup2 if(dup2(file,1) < 0) return 1; //Now standard out has been redirected, we can write to // the file printf( "This will print in myfile.txt\n" ); return 0; break; }//end of function main case 5: { { int i = 1; pid_t child_pid; printf("The main program process ID is %d", (int) getpid()); printf("%d", i); child_pid = fork(); if (child_pid != 0) { i++; printf("%d", i); printf("This is the parent process, with ID %d \n", (int) getpid()); printf("The child process is %d ", (int) child_pid); } else { printf("%d", i); printf("This is the child process, with ID %d \n", (int) getpid()); } } return 1; break; } default; {printf("You are out of prog\n"); } } }while(menu!=6); return 0; }
Subscribe to:
Post Comments (Atom)
Post A Comment:
0 comments: