Embedded Linux multi-process programming
The main program displays a text menu that provides the following services, requiring each service to be provided by generating child processes.
Services include: Calendar information display, date information display, Judgment Leap year service, file copy function, digital sorting function, Exit function.
Code and documentation (with flowchart): http://download.csdn.net/download/jingjingxujiayou/7540893
#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <time.h> #include < fcntl.h> #include <sys/stat.h>void displaycalen ();//1 Show calendar void Displaydate (); 2 Display date void Leapyear ();//3 judgment Leap Year void CopyFile ();//4 file assignment function void Sortnum (); 5 to number sort int start ();//Start Interface void menu (); menu function int Start () {int n; printf ("-----Linux multi-process programming-----\ n"); printf ("Display of the 1st calendar information \ n"); printf ("Display of information for the period 2nd \ n"); printf ("3 Judgment leap Year information service \ n"); printf ("Assignment function for 4 files \ n"); printf ("5 numbers sorted \ n"); printf ("6 exit program \ n"); printf ("Please input your choose (1-6)"); scanf ("%d", &n); return n;} void Displaycalen () {EXECLP ("cal", "Cal", "-sy", (char *) 0);} 2 Displays the current system date void Displaydate () {struct TM *ptr; time_t it; It=time (NULL); Ptr=localtime (&it); printf ("%4d%02d month%02d day%d:%d:%d\n", ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr- >TM_MIN,PTR->TM_SEC);} 3 judgement leap Year void Leapyear () {int m; printf ("Please input your yeaRS "); scanf ("%d", &m); if (((0==m%4) && (0!=m%100)) | | (0==m%400)) {printf ("%d is a leap year \ n", m); }else{printf ("%d is not a leap year \ n", m); }}//4 Copy file void CopyFile () {const char* pathfile = "file1.c"; int In,out,flag; Char buffer[1024]; in = Open ("file2.c", O_rdonly, S_IRUSR); if ( -1 = = in) {printf ("Open file file2.c error!\n"); Return } out = creat (PATHFILE,S_IWUSR); if ( -1 = = out) {printf ("Create file%s error!\n", pathfile); Return } while ((flag = Read (in,buffer,1024)) >0) {write (Out,buffer,flag); } close (in); Close (out); printf ("Copy file file2.c to%s\n", pathfile);} 5 Sort the numbers void Sortnum () {int b[10]={29,59,8,9,16,7,2,98,29,10}; int i,j,t,k; printf ("10 numbers in the array: \ n"); for (i = 0; i <; i + +) {printf ("%d\t", B[i]); } printf ("\ n"); for (i=0;i<10-1;i++) for (k=i,j=i+1;j<10;j++) {if (B[k]<b[j]) { K=j; } if (i!=k) {t=b[i]; B[I]=B[K]; b[k]=t; }} printf ("From large to small in order; \ n"); for (i=0;i<10;i++) printf ("%d\t", B[i]); printf ("\ n");} void menu () {int choose, k=1; pid_t child; while (1) {Choose=start (); Switch (choose) {Case 1:if ((Child=fork ()) ==-1) {printf ("error......\n"); }else if (child==0) {Displaycalen (); }else if (Child > 0) {waitpid (child, NULL, 0); } break; Case 2:if ((Child=fork ()) ==-1) {printf ("error......\n"); }else if (child==0) {displaydate (); }else if (Child > 0) {waitpid (child, NULL, 0); } break; Case 3:if ((Child=fork ()) ==-1) {printf ("error......\n"); }else if (child==0) { Leapyear (); }else if (Child > 0) {waitpid (child, NULL, 0); } break; Case 4:if ((Child=fork ()) ==-1) {printf ("error......\n"); }else if (child>0) {CopyFile (); }else if (child = = 0) {waitpid (child, NULL, 0); } break; Case 5:if ((Child=fork ()) ==-1) {printf ("error......\n"); }else if (child==0) {sortnum (); }else if (Child > 0) {waitpid (child, NULL, 0); } break; Case 6:system ("exit"); Break Default:break; }}}int Main () {menu (); return 0;}