rendered paste body/*************************************************************************** * cron_client.c * * Wed May 4 20:03:01 2005 * Copyright 2005 tw03d024 * Email tw03d024@technikum-wien.at ****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <time.h>#include <string.h>#define KEY 0410257051#define PERM 0666#define MAX_DATA 255struct tm tm_now;typedef struct{long mType;char mText[MAX_DATA];} message_t;int main(int argc, char *argv[]){ message_t msg; int msgid = -1; char command[MAX_DATA]; int i; char delim[] = ":"; char *token = NULL; int new_hour; int new_min; if (argc < 3) { fprintf(stderr,"Usage: %s hh:mm program [arg1] [agr2...argn]\n",argv[0]); return EXIT_FAILURE; } for(i=2; i<argc; i++) { strcat(command, argv[i]); strcat(command, " "); } token = strtok( argv[1], delim ); new_hour = atoi(token); token = strtok( NULL, delim); new_min = atoi(token); tm_now.tm_hour = new_hour; tm_now.tm_min = new_min; msg.mType = mktime(&tm_now); /* Message Queue oeffnen */ if( (msgid = msgget(KEY,PERM))==-1 ) { /* error handling */ fprintf(stderr,"%s: Can't access message queue\n",argv[0]); return EXIT_FAILURE; } /*Kommandozeile ins struct schreiben*/ strncpy(msg.mText,command,MAX_DATA); printf("%d\n", msgid); /* Nachricht verschicken */ if (msgsnd(msgid,&msg,sizeof(msg)-sizeof(long),0) == -1) { /* error handling */ fprintf(stderr,"%s: Can't send message\n",argv[0]); return EXIT_FAILURE; } fprintf(stdout, "Message sent %s\n",msg.mText); return EXIT_SUCCESS;}/*************************************************************************** * cron_server.c * * Wed May 4 20:03:01 2005 * Copyright 2005 tw03d024 * Email tw03d024@technikum-wien.at ****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <time.h>#include <string.h>#include <signal.h>#define KEY 0410257051#define PERM 0666#define MAX_DATA 255time_t timestamp;struct tm tm_now;typedef struct{long mType;char mText[MAX_DATA];} message_t;/*void myhandler(int sig);void myhandler(int sig){ if((msgctl(msgid, IPC_RMID, NULL) == -1) { fprintf("Message Queue couldn't be closed!\n",sig); fflush(stdout); } fprintf("Message Queue was shut down properly!\n"); fflusch(stdout); }}*/int main(int argc, char *argv[]){ message_t msg; int msgid = -1; time_t start, stop; double diff; start = time(NULL); if ( (msgid = msgget( KEY,PERM | IPC_CREAT | IPC_EXCL )) == -1 ) { fprintf(stderr, "%s: MessageQueue couldn't be started!\n", argv[0]); return EXIT_FAILURE; } diff=difftime(stop=time(NULL),start); mktime(&tm_now); timestamp = mktime(&tm_now); while(1) { /*(void) signal(SIGINT,myhandler);*/ fprintf(stdout, "Time: %d\n", (int)diff); fflush(stdout); while((diff=difftime(stop=time(NULL),start)) != 5); { if (msgrcv(msgid,&msg,sizeof(msg)-sizeof(long), timestamp, 0) == -1) { fprintf(stderr, "%s: Message couldn't be recieved!\n", argv[0]); return EXIT_FAILURE; } fprintf(stdout, "Message: %s %ld\n", msg.mText, (long)msg.mType); fflush (stdout); } } system("PAUSE"); return 0;}