Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/resource.h>
- #define BILLION 1E9
- /* for simplicity the # of procs is defined here,
- But a better idea is to read it from the command line */
- #define PROCESSES 4
- main()
- {
- int i, pid;
- /* Take initial time here */
- /* Use clock_gettime(). Do NOT use clock() */
- struct timespec requestStart, requestEnd;
- clock_gettime(CLOCK_REALTIME, &requestStart);
- for(i=0; i<PROCESSES; i++)
- {
- pid = fork();
- if(pid < 0) /* some error occurred – fork failed */
- {
- printf("Error");
- exit(-1);
- }
- if(pid == 0) /* child process code */
- {
- do_work(i, i+1, 20000, PROCESSES);
- exit(0);
- }
- /* do not place any wait() call here */
- }
- /* wait for all processes to finish their execution */
- for(i=0; i<PROCESSES; i++)
- {
- wait(NULL);
- }
- clock_gettime(CLOCK_REALTIME, &requestEnd);
- double accum = ( requestEnd.tv_sec - requestStart.tv_sec ) + ( requestEnd.tv_nsec - requestStart.tv_nsec ) / BILLION;
- printf( "Time = %lf\n", accum );
- /* Take final time here */
- /* Use clock_gettime(). Do NOT use clock() */
- /* Compute the execution time*/
- }
- /* this function is executed by each process */
- do_work(int nrProc, int inceput, int b, int pas)
- {
- int i, j, suma_div;
- for (i = inceput; i <= b; i += pas) {
- suma_div = 0;
- for (j = 1; j <= i / 2; j++) {
- if (i % j == 0) {
- suma_div += j;
- }
- }
- if (suma_div == i) {
- printf("Proces %d: %d \n", nrProc, i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement