



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This c program reads and validates the input of social security numbers (ssn) and home-phone numbers of students, checks if the ssn is already registered, and writes the new record to files 'dept.dat' and 'emps.dat'.
What you will learn
Typology: Essays (high school)
1 / 6
This page cannot be seen from the preview
Don't miss anything!
#include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <sys/wait.h>
int main(void) { FILE *f, *fi, *fp; int p[2], n=0, i, pid, pid2, status, flag = 0, pos = 5, len=0; long int ssn, phone; char nm[80], department[20]; Loop: (^) if ( flag == -1 )int opt; printf("\nPlease make sure that:\nSSN is 9 digits\nHome-Phone is 10 digits\n\n"); printf("Select one option\n1. New Record\n2. Exit\n Enter your choice:"); scanf("%d", &opt);
switch(opt) { case 1: printf("\nSocial Security Number(nine digits number):"); scanf("%ld", &ssn); printf("\nName:"); scanf("%s", nm); printf("\nDepartment:"); scanf("%s", department); printf("\nHome Phone(10 digits number):");
scanf("%ld", &phone); break; case 2: exit(2); }
int name_len = strlen(nm); int dept_len = strlen(department);
pipe(p); /* setting up the pipe / if ((pid = fork()) ==0) / in child / { //printf("\nChild 1 fork successful and the pid for the child is: %d\n", getpid()); close(p[1]); / child closes p[1] */ dup2(p[0], 0); close(p[0]); read(p[0], &ssn, sizeof(ssn)); read(p[0], nm, name_len); read(p[0], &phone, sizeof(phone)); read(p[0], department, dept_len); long int ssn_len_check = ssn, phone_len_check = phone; while(ssn_len_check != 0) { ssn_len_check = ssn_len_check/10; len++; } if(len != 9) {
goto Loop; } else {pos = pos + 9; Loop1: while(!feof(fi)) { c = fgetc(fi); pos++; if(c == '\n') { pos = pos + 6; fseek(fi, pos, SEEK_SET); fgets(sn, sizeof(sn)+2, fi); ssn1 = atoi(sn); if(ssn1 == ssn) { printf("\nThe SSN entered is already registered with us.\n\n"); goto Loop; } pos = pos + 8; } } } fclose(fi); } f=fopen("emps.dat", "a"); fprintf(f ,"SSN:\t%ld \t\tName:\t%s \t\tHome-Phone:\t%ld \n",ssn,nm,phone);
fclose(f); fp=fopen("dept.dat", "a"); fprintf(fp ,"SSN:\t%ld \t\tDepartment: \t %s \n",ssn, department); fclose(fp); _exit(0); /* child terminates */ }
if ((pid2 = fork ()) == 0) /* child two */
{ printf("\nChild 2 fork successful and the pid for the child is: %d\n", getpid()); close(p[0]); dup2(p[1],1); /* 1 becomes a duplicate of p[1] / close(p[1]); write(p[1], &ssn, sizeof(ssn)); write(p[1], nm, strlen(nm)); write(p[1], &phone, sizeof(phone)); write(p[1], department, strlen(department)); } close(p[0]); close(p[1]); / finished writing p[1] */
while (wait(&status)!=pid); /* waiting for pid */ if (status == 0) printf("child finished\n"); else printf("child failed\n"); return(0); }