Demo entry 6318235

c

   

Submitted by anonymous on Nov 06, 2016 at 14:26
Language: C. Code size: 946 Bytes.

#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
	pid_t pid;
	printf("P1\n");
	while ((pid = fork()) == -1);
	if (pid == 0)
	{
		double cost;
		struct timespec start, end;
		clock_gettime(CLOCK_MONOTONIC, &start);
		while (1) {
			clock_gettime(CLOCK_MONOTONIC, &end);
			cost = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
			if (cost > 1.01394)
			{
				break;
			}
		}
		printf("Pc cost %f sec\n", cost);
		exit(pid);
	}
	else
	{
		double cost;
		struct timespec start, end;
		clock_gettime(CLOCK_MONOTONIC, &start);
		while(1){
			clock_gettime(CLOCK_MONOTONIC, &end);
			cost = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
			if (cost > 1.51394)
			{
				break;
			}
		}
		printf("P2 cost %f sec\n", cost);
		wait(NULL);
	}
	printf("P3\n");
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).