0% found this document useful (0 votes)
22 views2 pages

C Program for Process Management and Time Display

The document is a C program that demonstrates the use of processes and time functions. It creates a child process using fork(), where both parent and child processes display their local and global variable values along with the current date and time. The program also includes a structure definition for time representation and handles memory allocation for time conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

C Program for Process Management and Time Display

The document is a C program that demonstrates the use of processes and time functions. It creates a child process using fork(), where both parent and child processes display their local and global variable values along with the current date and time. The program also includes a structure definition for time representation and handles memory allocation for time conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <unistd.

h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>

/*struct tm
* {
* int tm_sec; Seconds. [0-60] (1 leap second)
* int tm_min; Minutes. [0-59]
* int tm_hour; Hours. [0-23]
* int tm_mday; Day. [1-31]
* int tm_mon; Month. [0-11]
* int tm_year; Year - 1900.
* int tm_wday; Day of week. [0-6]
* int tm_yday; Days in year.[0-365]
* int tm_isdst; DST. [-1/0/1]
*
* # ifdef __USE_MISC
* long int tm_gmtoff; Seconds east of UTC.
* const char *tm_zone; Timezone abbreviation.
* # else
* long int __tm_gmtoff; Seconds east of UTC.
* const char *__tm_zone; Timezone abbreviation.
* # endif
* };
* */

struct tm t;
int var_glb; /* variable globale */

void affiche_dh(char ch)


{
struct tm *tn ;
char *
mois[12]={"Jan","Fev","Mar","Avr","Mai","Jun","Jul","Aout","Sep","Oct","Nov","Dec"}
;
char * jour[7]={"Dim","Lun","Mar","Mer","Jeu","Ven","Sam"};

/* lire l'heure courante */


time_t now = time (NULL);
printf(" Il fait %d sec depuis le 1/1/1970 a minuit\n\n",now);

/* la convertir en heure locale */


tn=(struct tm *) malloc(sizeof(struct tm));
tn= localtime (&now);
printf("Fin du processus %c le %s. %d %s ",ch,jour[tn->tm_wday],tn-
>tm_mday,mois[tn->tm_mon]);
printf("%d:%d:%d ", tn->tm_hour,tn->tm_min,tn->tm_sec);
printf("%s %d\n",tn->tm_zone,tn->tm_year+1900);

int main(void)
{
int pidfils;
int var_lcl = 0; // Variable locale
system("clear");
pidfils = fork();

switch (pidfils)
{
case -1:
// Echec de fork
printf("\n Fork a echoue !!!!!!\n"); return 1;

case 0: // Processus fils


var_lcl++; var_glb++;
printf("\n Processus fils:%d: var_lcl = [%d], var_glb = [%d]\n",
getpid(), var_lcl, var_glb);
affiche_dh('F');
break;

default: //Processus Parent


var_lcl = 10; var_glb = 20;
printf("\n Processus Parent :%d: var_lcl = [%d], var_glb = [%d]\n",
getpid(), var_lcl, var_glb);
sleep(5);
affiche_dh('P');
}

printf("Fin du programme \n");return 0;


}

int var_lcl = 0; // Variable locale


system("clear");
pidfils = fork();

You might also like