Advertisement
Guest User

Server

a guest
May 26th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <time.h>
  10.  
  11. #define SHMSZ 27
  12.  
  13. struct ids {
  14.     pid_t pid;
  15.     uid_t p_uid;
  16.     uid_t p_gid;
  17. };
  18.  
  19. int main(int argc, char * argv[]) {
  20.     struct ids process;
  21.     key_t key; /*Name of shared memory segment*/
  22.     int shmid;
  23.     time_t timer, stime;
  24.     double load[3];
  25.     int *shm;
  26.     int *data;
  27.     int c;
  28.    
  29.     stime = time(NULL);
  30.     process.pid = getpid();
  31.     process.p_uid = getuid();
  32.     process.p_gid = getgid();
  33.     key = 669;
  34.  
  35.     /*Create the segment*/
  36.         if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
  37.             perror(strerror(errno));
  38.             exit(1);
  39.         }
  40.  
  41.     /*Attach the segment to our data space*/
  42.         if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
  43.             perror(strerror(errno));
  44.             exit(1);
  45.         }
  46.  
  47.         /*Put some things into the memory for the other process to read*/
  48.         data = shm;
  49.    
  50.     *data++ = process.pid;
  51.     *data++ = process.p_uid;
  52.     *data++ = process.p_gid;
  53.  
  54.     /*printf("%i\n", data[shm]);
  55.     printf("%i\n", data[shm + 1]);
  56.     printf("%i\n", data[shm + 2]);*/
  57.  
  58.         *data = NULL;
  59.    
  60.     for(;;) {
  61.         if (getloadavg(load, 3) < 0) {
  62.             perror(strerror(errno));
  63.             exit(1);   
  64.         }
  65.         else {
  66.             printf("Load average:\t1m - %f\t5m - %f\t15m - %f\t\n", load[0], load[1], load[2]);
  67.             timer = time(NULL);
  68.             printf("Time:\t%.0f\n", difftime(timer, stime));
  69.         }
  70.         sleep(1);
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement