Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #define SHMSZ 27
- struct ids {
- pid_t pid;
- uid_t p_uid;
- uid_t p_gid;
- };
- int main(int argc, char * argv[]) {
- struct ids process;
- key_t key; /*Name of shared memory segment*/
- int shmid;
- time_t timer, stime;
- double load[3];
- int *shm;
- int *data;
- int c;
- stime = time(NULL);
- process.pid = getpid();
- process.p_uid = getuid();
- process.p_gid = getgid();
- key = 669;
- /*Create the segment*/
- if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
- perror(strerror(errno));
- exit(1);
- }
- /*Attach the segment to our data space*/
- if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
- perror(strerror(errno));
- exit(1);
- }
- /*Put some things into the memory for the other process to read*/
- data = shm;
- *data++ = process.pid;
- *data++ = process.p_uid;
- *data++ = process.p_gid;
- /*printf("%i\n", data[shm]);
- printf("%i\n", data[shm + 1]);
- printf("%i\n", data[shm + 2]);*/
- *data = NULL;
- for(;;) {
- if (getloadavg(load, 3) < 0) {
- perror(strerror(errno));
- exit(1);
- }
- else {
- printf("Load average:\t1m - %f\t5m - %f\t15m - %f\t\n", load[0], load[1], load[2]);
- timer = time(NULL);
- printf("Time:\t%.0f\n", difftime(timer, stime));
- }
- sleep(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement