Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- options.c
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc, char *argv[]) {
- int i;
- /*
- for (i=0; i < argc; i++) {
- printf("parametrem nr %2d jest: %s\n", i, argv[i]);
- }
- */
- extern int opterr;
- opterr=0;
- extern char *optarg;
- int ch;
- while ((ch = getopt(argc, argv, "xf:")) != -1) {
- switch (ch) {
- case 'x':
- printf("Podano opcjÄ™ x\n");
- break;
- case 'f':
- printf ("opcja f o wartosci '%s'\n", optarg);
- break;
- case '?':
- default:
- printf ("uzycie -x albo -f ...\n");
- }
- }
- }
- envvar.c
- #include <stdio.h>
- #include <stdlib.h>
- int main( int argc, char* argv[], char *envp[]) {
- //while(*envp)
- //printf("%s\n",*envp++);
- //char *user;
- //user = getenv("USER");
- //if (user!=NULL) printf("Username: %s\n", user);
- setenv("X", "ABC", 1);
- //char *X;
- //X = getenv("X");
- //if (X!=NULL) printf("X=%s\n", X);
- extern char **environ;
- while(*environ)
- printf("%s\n",*environ++);
- }
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- int main(int argc, char *argv[]) {
- printf("uid=%d\n", getuid());
- printf("euid=%d\n", geteuid());
- printf("gid=%d\n", getgid());
- printf("egid=%d\n", getegid());
- }
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- int main (void) {
- struct stat buf;
- int x;
- x = stat ("link", &buf);
- if (S_ISLNK(buf.st_mode)) printf (" stat mowi: link\n");
- if (S_ISREG(buf.st_mode)) printf (" stat mowi: plik\n");
- x = lstat ("link", &buf);
- if (S_ISLNK(buf.st_mode)) printf ("lstat mowi: link\n");
- if (S_ISREG(buf.st_mode)) printf ("lstat mowi: plik\n");
- return 0;
- }
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- int main(int argc, char *argv[]) {
- int wej, wyj, n;
- char bufor[100];
- wej=open(argv[1], O_RDONLY);
- wyj=open(argv[2], O_CREAT| O_WRONLY | O_TRUNC, S_IRWXU);
- //wyj=open(argv[2], O_CREAT| O_WRONLY | O_TRUNC);
- while ((n=read(wej, bufor, sizeof(bufor)))!=0) {
- //printf("%d", n);
- write(wyj, bufor, n);
- }
- close(wej);
- close(wyj);
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <limits.h>
- void readdir2(char *name) {
- DIR *dirp = opendir(name);
- struct dirent* entry;
- int newpath_length;
- char newpath[PATH_MAX];
- if (dirp != NULL) {
- while ((entry = readdir(dirp)) != NULL) {
- if (entry->d_type == DT_DIR ) {
- printf("%s\n", entry->d_name);
- newpath_length = snprintf (newpath, PATH_MAX, "%s/%s", name, entry->d_name);
- if (newpath_length >= PATH_MAX) {
- perror("Zbyt dluga sciezkaa.\n");
- exit(EXIT_FAILURE);
- }
- if (!(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0))
- readdir2(newpath);
- }
- }
- }
- closedir(dirp);
- }
- int main( void )
- {
- readdir2(".");
- return EXIT_SUCCESS;
- }
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- int main(int argc, char *argv[]) {
- int wyj, n, newout;
- char bufor[100];
- newout=dup(1);
- close(1);
- wyj=open("tajemniczyplik.txt", O_CREAT| O_WRONLY | O_TRUNC, S_IRUSR|S_IRWXU);
- //printf("%d", wyj);
- while ((n=read(0, bufor, 100))!=0) {
- write(1, bufor, n);
- write(newout, bufor, n);
- }
- close(wyj);
- }
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- int main(int argc, char **argv)
- {
- int in, out;
- char *args[] = {"grep", "lancuch", NULL};
- in = open("plikwej.txt", O_RDONLY);
- out = open("plikwyj.txt", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
- dup2(in, 0);
- dup2(out, 1);
- close(in);
- close(out);
- execvp("grep", args);
- }
- #include <stdio.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <unistd.h>
- void obsluga(int sig) {
- printf("parent: otrzymalem sygnal %d\n", sig);
- //signal(SIGINT, obsluga);
- }
- int main(void) {
- pid_t pid;
- printf("zaczyna przodek\n");
- signal(SIGINT, obsluga);
- printf("obsluga ustawiona\n");
- //sleep(10);
- if ((pid = fork())==0) {
- //potomek
- printf("zaczyna potomek\n");
- kill(getppid(), SIGINT);
- //kill(getppid(), SIGINT);
- printf("potomek konczy\n");
- } else if (pid>0) {
- //przodek
- waitpid(pid);
- printf("przodek konczy\n");
- return EXIT_SUCCESS;
- } else if (pid==-1) {
- printf("Nie mozna utworzyc procesu");
- exit(EXIT_FAILURE);
- }
- }
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- void obsluga (int sig) {
- printf("budzik\n");
- }
- int main() {
- signal(SIGALRM, obsluga);
- alarm(5);
- //alarm(4);
- while(1);
- return 0;
- }
- #include <signal.h>
- #include <stdlib.h>
- #include <stdio.h>
- void obsluga(int sygnal) {
- printf("odebralem sygnal %d\n", sygnal);
- }
- int main() {
- signal(SIGINT, obsluga);
- printf("wysylam sygnal do siebie\n");
- raise(SIGINT);
- return EXIT_SUCCESS;
- }
- #include <signal.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- void obsluga(int sygnal) {
- printf("Wcisnales ctl-c\n");
- }
- int main() {
- struct sigaction act, oldact;
- act.sa_handler = obsluga;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- sigaction(SIGINT, &act, &oldact);
- while(1);
- return EXIT_SUCCESS;
- }
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- int main() {
- sigset_t sset;
- sigemptyset(&sset);
- sigaddset(&sset, SIGQUIT);
- sigaddset(&sset, SIGALRM);
- sigaddset(&sset, SIGINT);
- sigprocmask(SIG_BLOCK, &sset, NULL);
- alarm(2);
- while(1);
- return 0;
- }
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <stdlib.h>
- int i=10;
- void obsluga (int sig) {
- printf("budzik %d\n", i);
- i--;
- if (i>=1) alarm(1); else exit(0);
- }
- int main() {
- struct sigaction act, oldact;
- act.sa_handler = obsluga;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- sigaction(SIGALRM, &act, &oldact);
- alarm(1);
- while (1) pause();
- return 0;
- }
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define MAXLINE 255
- main() {
- int n;
- pid_t pid;
- int lacze[2];
- char line[MAXLINE], command[MAXLINE]="test";
- if (pipe(lacze)==-1) {
- perror("nie mozna utworzyc lacza");
- exit(1);
- }
- if ((pid=fork())>0) {
- close(lacze[0]);
- //sleep(5);
- write(lacze[1], command, sizeof(command));
- } else if (pid==0) {
- close(lacze[1]);
- n=read(lacze[0], line, MAXLINE);
- line[n]='\0';
- //printf("potomek po read\n");
- printf("potomek: dane z pipe:%s\n", line);
- } else {
- perror("nie można utworzyc potomka");
- exit(2);
- }
- exit(EXIT_SUCCESS);
- }
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define MAXLINE 255
- main() {
- int n;
- char line[MAXLINE], command[MAXLINE], expr[MAXLINE];
- FILE *fp;
- scanf("%s", expr);
- sprintf(command, "echo \"");
- strcat(command, expr);
- strcat(command, "\"|bc -l");
- //printf("%s", command);
- if ( (fp = popen(command, "r")) == NULL)
- perror("popen error");
- fgets(line, MAXLINE, fp);
- line[strlen(line)-1]='\0';
- if (ferror(fp))
- perror("fgets error");
- printf("wynik: %s\n", line);
- pclose(fp);
- exit(EXIT_SUCCESS);
- }
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define MAXLINE 255
- main() {
- char line[MAXLINE], command[MAXLINE], expr[MAXLINE];
- FILE *fp;
- scanf("%s", expr);
- sprintf(command, "bc");
- strcat(expr, "\n");
- if ( (fp = popen(command, "w")) == NULL)
- perror("popen error");
- fwrite(expr, sizeof(char), strlen(expr), fp);
- if (ferror(fp))
- perror("fgets error");
- pclose(fp);
- exit(EXIT_SUCCESS);
- }
- #include "naglowek.h"
- main(argc, argv)
- int argc;
- char* argv[];
- {
- char nazwa[MAXBUF], dane[MAXBUF], bufor[MAXBUF];
- int rdfifo, wrfifo; // To beda deskryptory
- int n;
- KOMUNIKAT komunikat;
- // ----- Przygotowanie komunikatu do wyslania ------
- printf("C: Wpisz teskst i zakoncz przez ^D\n");
- while ( fgets(bufor, MAXBUF, stdin) != NULL)
- sprintf(komunikat.txt, "%s", bufor);
- komunikat.pid = getpid();
- // ----- Tworze fifo tymczasowe do czytania ------
- sprintf(nazwa, "/tmp/jerzyfifo%d", getpid());
- //umask(0000);
- mkfifo(nazwa,0777);
- // ----- Otwieram fifo serwera ---------
- if ((wrfifo = open(FIFO, O_WRONLY)) < 0)
- perror("C: Nie moge otworzyc FIFO");
- else
- printf("C: Otworzylem: %s\n", FIFO);
- // ------ Wyslanie i odebranie komunikatu -------
- if ((n = write(wrfifo, &komunikat, sizeof(komunikat))) < 0)
- perror("C: Blad pisania do fifo");
- printf("\nC: Moj pid = %d, wysylam komunikat: %s\n", komunikat.pid, komunikat.txt);
- // ----- Otwieram wlasne fifo ---------
- if ((rdfifo = open(nazwa, O_RDONLY)) < 0)
- perror("C: Nie moge otworzyc fifo\n");
- else
- printf("C: Otworzylem: %s\n", nazwa);
- if ((n = read(rdfifo, &komunikat, sizeof(komunikat))) < 0)
- perror("C: Blad czytania z fifo");
- printf("\nC: Odebralem komunikat: %s\n", komunikat.txt);
- if (unlink(nazwa)==-1) perror("C: Blad usuwania fifo.\n");
- else printf("C: Usunalem fifo: %s\n", nazwa);
- return 0;
- }
- #include "naglowek.h"
- #include <sys/fcntl.h>
- #include <wait.h>
- #include <ctype.h>
- #include <string.h>
- void children_handle(int sygnal) {
- pid_t pid;
- int stat;
- while (waitpid((pid_t)(-1), &stat, WNOHANG) > 0) {}
- }
- void obsluga(KOMUNIKAT *komunikat) {
- int wrfifo; // deskryptor fifo do pisania
- char nazwa[MAXBUF];
- printf("S: Otrzymalem komunikat: %s\n", komunikat->txt);
- sprintf(nazwa, "/tmp/jerzyfifo%d", komunikat->pid);
- printf("S: Wysylam odpowiedz do %s\n", nazwa);
- if((wrfifo = open(nazwa, O_WRONLY)) < 0)
- perror("S: Nie moge otworzyc fifo klienta\n");
- else
- printf("S: Otworzylem: %s\n", nazwa);
- if(write(wrfifo, komunikat, sizeof(komunikat)) < 0)
- perror("S: Nie moge pisac do fifo\n");
- else
- printf("S: Wyslalem tekst: %s do %s\n", komunikat->txt, nazwa);
- close(wrfifo);
- }
- serwer() {
- int pid, rdfifo, status, n;
- KOMUNIKAT komunikat;
- // ----- Tworze i otwieram fifo do czytania -----
- printf("S: Czekam na klienta!!\n");
- // umask(0000);
- if (mkfifo(FIFO, 0666) < 0)
- perror("S: Nie moge stworzyc FIFO\n");
- else
- printf("S: Utworzylem fifo: %s\n", FIFO);
- if ((rdfifo = open(FIFO, O_RDONLY)) < 0)
- perror("S: Nie moge otworzyc FIFO\n");
- else
- printf("S: Otworzylem fifo do czytania\n");
- // ----- Nieskonczona petla obslugujaca klientow -----
- for(;;) {
- // ----- Czytam komunikat z fifo -----
- n=read(rdfifo, &komunikat, sizeof(komunikat));
- if (n==0) {
- close(rdfifo);
- printf("S: Czekam na klienta!!\n");
- rdfifo = open(FIFO, O_RDONLY);
- } else
- if((pid = fork()) < 0)
- perror("S: nie moge stworzyc nowego procesu\n");
- else {
- if(pid == 0) {
- obsluga(&komunikat);
- //sleep(10);
- printf("S: Obsluzylem klienta!!\n");
- exit(0);
- }
- }
- }
- };
- main() {
- pid_t pid1;
- struct sigaction act, oldact;
- //usuwamy potencjalne fifo z poprzedniego uruchomienia
- unlink(FIFO);
- if((pid1 = fork()) < 0)
- perror("S: Nie moge stworzyc nowego procesu\n");
- else
- if(pid1 == 0) { //proces serwera
- //ustanawiamy obsluge syngnalu SIGCHLD
- act.sa_handler = children_handle;
- sigemptyset(&act.sa_mask);
- act.sa_flags = SA_RESTART | SA_NOCLDSTOP;
- sigaction(SIGCHLD, &act, &oldact);
- //uruchamiamy funkcje glowna serwera
- serwer();
- }
- else printf("S: Serwer dziala w tle\n");
- }
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- #include <wait.h>
- #include <ctype.h>
- #include <stdlib.h>
- #define FIFO "/tmp/jerzy_fifo"
- #define MAXBUF 256
- typedef struct
- {
- int pid;
- char txt[MAXBUF];
- } KOMUNIKAT;
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/types.h>
- #include <stdio.h>
- #define MAXBUF 256
- #define MSG_4_SERV 1
- #define KLUCZ 20160117L
- typedef struct {
- long type; // typ komunikatu
- pid_t pid; // kto wysyla
- float a,b;
- char oper;
- } KOMUNIKAT;
- #include "naglowek.h"
- void wypisz_dane(int msqid)
- {
- struct msqid_ds dane;
- if (msgctl(msqid, IPC_STAT, &dane) < 0)
- perror("Blad przy pobieraniu danych o kolejce\n");
- printf("Dane o kolejce o deskr %d:\n", msqid);
- printf("biez. l. bajtow w kolejce: %d\n", dane.msg_cbytes);
- printf("biez. l. komunikatow w kolejce: %d\n", dane.msg_qnum);
- printf("max. l. bajtow dla kolejki: %d\n", dane.msg_qbytes);
- printf("id. procesu, ktory ostatnio wywolal f-cje msgsnd: %d\n", dane.msg_lspid);
- printf("id. procesu, ktory ostatnio wywolal f-cje msgrcv: %d\n", dane.msg_lrpid);
- printf("czas ostat. wywol. f-cji msgrcv: %s", ctime(dane.msg_stime) );
- printf("czas ostat. wywol. f-cji msgsnd: %s", ctime(dane.msg_rtime) );
- printf("id. uzytkownika dla wlasciciela: %d\n", dane.msg_perm.uid);
- printf("id. grupy dla wlasciciela: %d\n", dane.msg_perm.gid);
- printf("id. uzytkownika dla tworcy: %d\n", dane.msg_perm.cuid);
- printf("id. grupy dla tworcy: %d\n", dane.msg_perm.cgid);
- //printf("Numer kolejny: %d\n", dane.msg_perm.seq);
- printf("Tryb dostepu do kolejki: %o\n", dane.msg_perm.mode & 0777);
- }
- serwer()
- {
- int msqid ,rozmiar_kom;
- KOMUNIKAT komunikat;
- float licz1, licz2, wynik;
- char dzial;
- rozmiar_kom = sizeof(komunikat)-sizeof(long);
- if ((msqid=msgget(KLUCZ, 0666 | IPC_CREAT | IPC_EXCL) ) < 0)
- perror("S: Blad tworzenia kolejki\n");
- else printf("S: Kolejka utworzona z deskryptorem %d\n", msqid);
- wypisz_dane(msqid);
- //sleep(40);
- for(;;)
- {
- msgrcv(msqid, &komunikat, rozmiar_kom, MSG_4_SERV, 0);
- dzial=komunikat.oper;
- licz1=komunikat.a;
- licz2=komunikat.b;
- printf("S: Odebralem komunikat od klienta. Dzialanie %c\n", dzial);
- switch(dzial)
- {
- case '+' : wynik=licz1+licz2; break;
- case '-' : wynik=licz1-licz2; break;
- case '*' : wynik=licz1*licz2; break;
- case '/' : wynik=licz1/licz2; break;
- default : {
- printf("Nie znam dzialania: %c\n", dzial);
- wynik=0;
- }
- }
- komunikat.a=wynik;
- komunikat.type=komunikat.pid;
- msgsnd(msqid, &komunikat, rozmiar_kom, 0);
- printf("S: Obsluzylem klienta.\n");
- }
- /*
- if (msgctl(msqid, IPC_RMID, (struct msqid_ds *) 0) < 0)
- perror("S: Nie moge usunac kolejki\n");
- else printf("S: Kolejka usunieta\n");
- */
- }
- main()
- {
- pid_t pid1;
- if((pid1 = fork()) < 0)
- perror("S: Nie moge stworzyc nowego procesu\n");
- else
- if(pid1 == 0) serwer();
- else printf("S: Serwer dziala w tle\n");
- }
- #include "naglowek.h"
- main() {
- int msqid, rozmiar_kom;
- char dzial[MAXBUF];
- float liczba;
- KOMUNIKAT komunikat;
- rozmiar_kom=sizeof(komunikat) - sizeof(long);
- if ((msqid = msgget(KLUCZ, 0666 | IPC_CREAT)) < 0)
- perror("C: Nie moge otworzyc kolejki serwera");
- else
- printf("C: Otworzylem kolejke serwera, id = %d.\n", msqid);
- // ----- Przygotowanie komunikatu do wyslania ------
- printf("C: Wpisz znak dzialania: ");
- if ( fgets(dzial, MAXBUF, stdin) == NULL) perror("C: Blad: fgets");
- komunikat.pid = getpid();
- komunikat.type = MSG_4_SERV;
- komunikat.oper=dzial[0];
- printf("C: Podaj liczbe a=");
- scanf("%f", &komunikat.a);
- printf("C: Podaj liczbe b=");
- scanf("%f", &komunikat.b);
- // ------ Wyslanie komunikatu do serwera -----------
- msgsnd(msqid, &komunikat, rozmiar_kom, 0);
- //------- Odebranie potwierdzenia ------------------
- msgrcv(msqid, &komunikat, rozmiar_kom, getpid(), 0);
- printf("C: Odebralem od serwera wynik: %f\n", komunikat.a);
- return 0;
- }
- /*********************************************
- *
- * program sem_create.c
- * tworzy semafor (jeden) o zadanym kluczu
- * wykonuje tez jedna operacje nadania mu wartosci 11
- *
- *********************************************/
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <stdio.h>
- #include <stdlib.h>
- int semid;
- int usun(int i) {
- if (semctl(semid, 0, IPC_RMID)!=-1)
- printf("Sygnal %d ....\n",i);
- else {
- printf("Nie mozna usunac semafora");
- exit(1);
- }
- }
- main(int argc, char *argv[]) {
- int i, klucz;
- struct sembuf ustaw[1] = { 0, 11, 0 };
- if (argc < 2) {
- printf ("Uzycie %s IPC_klucz\n", argv[0]);
- exit(1);
- }
- for (i=0; i< 18; i++)
- signal(i, usun);
- klucz = atoi(argv[1]);
- if ( (semid = semget(klucz, 1, 0777 | IPC_CREAT | IPC_EXCL) ) < 0) {
- perror("Blad tworzenia");
- exit(1);
- }
- printf("Semafor utworzony z identyfikatorem %d\n", semid);
- if (semop(semid, &ustaw[0], 1) < 0)
- perror("blad semop");
- pause();
- }
- /*************************************************
- *
- * program sem_stat.c
- * drukuje wartosc semafora o zadanym kluczu
- * zmniejsza wartosc semafora o 1
- *
- ************************************************/
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <stdio.h>
- #include <stdlib.h>
- main(argc, argv)
- int argc;
- char *argv[];
- {
- long semid, wart;
- key_t klucz;
- union semun {
- int val;
- struct semid_ds *buf;
- ushort *array;
- } arg;
- if (argc < 2) {
- printf("Uzycie %s IPC_klucz\n", argv[0]);
- exit(1);
- }
- klucz = atoi(argv[1]);
- if ( (semid = semget(klucz, 0, IPC_CREAT) ) < 0)
- {perror("Blad semget");
- exit(1);
- }
- if ( ( wart = semctl(semid, 0,GETVAL, arg)) < 0) {
- perror("Blad semctl2");
- exit(3);
- }
- printf("Aktualna wartosc semafora: %d\n",wart);
- if ( ( wart = semctl(semid, 0,GETPID, arg)) < 0) {
- perror("Blad semctl");
- exit(3);
- }
- printf("PID ostatniego procesu: %d\n",wart);
- struct sembuf ustaw[1] = { 0, -1, 0 };
- if (semop(semid, &ustaw[0], 1) < 0)
- perror("blad semop");
- if ( ( wart = semctl(semid, 0, GETVAL, arg)) < 0) {
- perror("Blad semctl2");
- exit(3);
- }
- printf("Aktualna wartosc semafora: %d\n",wart);
- if ( ( wart = semctl(semid, 0,GETPID, arg)) < 0) {
- perror("Blad semctl");
- exit(3);
- }
- printf("PID ostatniego procesu: %d\n",wart);
- exit(0);
- }
- #include <sys/shm.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- #include <sys/fcntl.h>
- #include <string.h>
- #define KLUCZ 20160107L
- #define L_KB 10
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include "naglowek.h"
- int shmid;
- void wypisz_dane(int shmid) {
- struct shmid_ds dane;
- if (shmctl(shmid, IPC_STAT, &dane) < 0)
- perror("Blad przy pobieraniu danych o kolejce\n");
- printf("Dane o pamieci wspolnej o deskr %d:\n", shmid);
- printf("rozmiar segmentu: %d\n", dane.shm_segsz);
- printf("id. procesu dla ostatniej operacji %d\n", dane.shm_lpid);
- printf("id. procesu tworcy: %d\n", dane.shm_cpid);
- printf("dolaczony nr biezacy: %d\n", dane.shm_nattch);
- printf("id. uzytkownika dla wlasciciela: %d\n", dane.shm_perm.uid);
- printf("id. grupy dla wlasciciela: %d\n", dane.shm_perm.gid);
- printf("id. uzytkownika dla tworcy: %d\n", dane.shm_perm.cuid);
- printf("id. grupy dla tworcy: %d\n", dane.shm_perm.cgid);
- printf("Tryb dostepu do kolejki: %o\n", dane.shm_perm.mode & 0777);
- }
- void obsluga_term() {
- if (shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0) < 0)
- perror("S: Nie moge usunac pamieci\n");
- else printf("S: Pamiec usunieta\n");
- exit(0);
- }
- serwer() {
- int rozmiar_kom;
- char *dane, *dane_tmp;
- char temp[100];
- int pass, n, klient;
- int i=0;
- signal(SIGTERM, obsluga_term); // ustawiam obsluge sygnalu TERM
- if ((shmid=shmget(KLUCZ, L_KB*1024, 0777 | IPC_CREAT | IPC_EXCL) ) < 0)
- perror("S: Blad tworzenia pamieci\n");
- else printf("S: Pamiec utworzona z deskryptorem %d\n", shmid);
- if ( (dane = dane_tmp = (char *) shmat(shmid, (char *) 0, 0) ) == (char *) -1)
- perror("S: Nie moge przylaczyc pamieci\n");
- pass=open("/etc/passwd", O_RDONLY);
- do {
- n=read(pass, temp, 100);
- //write(1, temp, n);
- i+=n;
- if (i>= L_KB*1024) break;
- memcpy(dane_tmp, temp, n);
- dane_tmp+=n;
- } while(n == 100);
- printf("S: Wczytalem %d bajtow z pliku /etc/passwd do pamieci\n", i);
- close(pass);
- if (((int) shmdt(dane)) == -1)
- perror("S:Nie moge odlaczyc pamieci\n");
- for(;;);
- //wypisz_dane(shmid);
- }
- main() {
- pid_t pid1;
- if((pid1 = fork()) < 0)
- perror("S: Nie moge stworzyc nowego procesu\n");
- else
- if(pid1 == 0) serwer();
- else printf("S: Serwer dziala w tle\n");
- // serwer();
- }
- #include "naglowek.h"
- #define ILE_W 21
- main() {
- int shmid;
- char *dane, *dane_tmp;
- int i=0,k,n;
- if ((shmid=shmget(KLUCZ, L_KB*1024, 0666 | IPC_CREAT) ) < 0)
- perror("C: Blad uchwytu pamieci\n");
- printf("C: dolaczylem do pamieci o id %d\n", shmid);
- if ( (dane = dane_tmp = (char *) shmat(shmid, NULL, SHM_RDONLY) ) == (char *) -1)
- perror("C: Nie moge przylaczyc pamieci\n");
- for(k=0; k < ILE_W; k++ ) {
- do {
- n=write(1, dane_tmp, 1);
- dane_tmp++;
- } while (*dane_tmp != '\n');
- }
- printf("\n");
- printf("C: Wypisalem %d wierszy pliku /etc/passwd\n", (int) ILE_W);
- if (shmdt((void *) dane) == -1)
- perror("C:Nie moge odlaczyc pamieci\n");
- }
- #include <stdio.h>
- #include <mqueue.h>
- main(int argc, char *argv[]) {
- int res;
- int kom=77;
- mqd_t mq;
- char *mq_name="/jerzy";
- // Utworzenie kolejki komunikatow
- if ((mq = mq_open(mq_name, O_RDWR | O_CREAT , 0660, NULL))==-1)
- perror("Blad tworzenia kolejki");
- // wysylanie komunikatu
- if ((res = mq_send(mq, (char *) &kom, sizeof(kom), 2)) == -1)
- perror("Nie moge wyslac komunikatu");
- printf("Wyslano komunikat: %d\n",kom);
- mq_close(mq);
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <mqueue.h>
- main(int argc, char *argv[]) {
- int i;
- int res;
- char *kom;
- long sizemax;
- unsigned int prio;
- mqd_t mq;
- struct mq_attr attr;
- char *mq_name="/jerzy";
- // Otwarcie kolejki komunikatow
- if ((mq = mq_open(mq_name, O_RDWR , 0660, NULL ))==-1)
- perror("Blad otwarcia kolejki");
- //pobranie informacji o kolejce komunikatow
- if (mq_getattr(mq, &attr)==-1)
- perror("Blad odczytu atrybutow kolejki");
- // pobranie maksymalnego rozmiaru komunikatu
- sizemax=attr.mq_msgsize;
- /* pobranie komunikatu */
- if ((kom=malloc(sizemax)) == NULL) {
- perror("Nie udalo sie przydzielic pamieci na komunikat\n");
- exit(1);
- }
- if ((res = mq_receive(mq, kom, sizemax, &prio))==-1)
- perror("Blad odczytu z kolejki");
- else printf("Otrzymano komunikat: %d\n", (int) *kom);
- free(kom);
- // zamkniecie kolejki
- mq_close(mq);
- // usuniecie kolejki
- if (res=mq_unlink(mq_name) !=-1 )
- printf("Usunalem kolejke: %s\n", mq_name);
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <semaphore.h>
- int main(int argc, char *argv[]) {
- sem_t *sem;
- int wartosc;
- char *sem_name="/jerzy";
- // otwarcie semafora
- sem = sem_open(sem_name, 0);
- // zwiekszenie wartosci semafora o jeden
- sem_post(sem);
- /* pobranie wartosci semafora */
- sem_getvalue(sem , &wartosc);
- printf("wartosc: %d\n", wartosc);
- /* zamkniecie semafora */
- sem_close(sem);
- //sem_unlink(sem_name);
- exit(0);
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/fcntl.h>
- #include <semaphore.h>
- /* przyklad blokowania semafora */
- int main(int argc, char *argv[]) {
- sem_t *sem;
- int wartosc;
- char *sem_name="/jerzy";
- // utworzenie semafora
- sem = sem_open(sem_name, O_RDWR | O_CREAT, 0644, 0);
- // oczekiwanie na zwiekszenie wartosci semafora
- sem_wait(sem);
- // pobranie wartosci semafora
- sem_getvalue(sem, &wartosc);
- printf("Wartosc: %d\n", wartosc);
- // zamkniecie semafora
- sem_close(sem);
- exit(0);
- }
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/fcntl.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- int main(int argc,char *argv[]) {
- int fd, i;
- char *wsk;
- char *shm_name="/jerzy";
- size_t shm_length=1024;
- // utworzenie i otwarcie pamieci wspolnej
- fd = shm_open(shm_name, O_RDWR | O_CREAT , 0644);
- ftruncate(fd, shm_length);
- // odwzorowanie pamieci w obszar pamieci procesu
- wsk = mmap(NULL, shm_length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- // zapis do pamieci
- for(i=0; i<shm_length; i++)
- *wsk++ = i % 256;
- //shm_unlink(shm_name); //usuniecie pamieci wspolnej
- exit(0);
- }
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/fcntl.h>
- #include <sys/mman.h>
- int main(int argc,char * argv[]) {
- int fd, mflagi, i;
- struct stat shm_status;
- unsigned char *wsk, c;
- char *shm_name="/jerzy";
- // otwarcie pamieci wspolnej
- fd = shm_open(shm_name, O_RDONLY , 0644);
- // pobranie informacji o pamieci wspolnej
- fstat(fd, &shm_status);
- // odwzorowanie pamieci fd w obszar pamieci procesu
- wsk = mmap(NULL, shm_status.st_size, PROT_READ, MAP_SHARED, fd, 0);
- //printf("Wielkosc pamieci: %d", shm_status.st_size);
- // odczyt z pamieci
- for(i=0; i< shm_status.st_size; i++) {
- if ( (c = *wsk++) != (i % 256))
- perror("Blad: ");
- else printf("%c-", c%128);
- }
- //usuniecie pamieci wspolnej
- if (i=shm_unlink(shm_name) !=-1 )
- printf("Usunalem pamiec wspolna: %s\n", shm_name);
- exit(0);
- }
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <stdlib.h>
- main() {
- int gniazdo_k, dlugosc_s, res;
- struct sockaddr_in adres_serwera;
- gniazdo_k=socket(AF_INET, SOCK_STREAM, 0);
- adres_serwera.sin_family=AF_INET;
- adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
- adres_serwera.sin_port=htons(11199); //port serwera
- dlugosc_s=sizeof(adres_serwera);
- //polaczenie z serwerem
- res=connect(gniazdo_k, (struct sockaddr *) &adres_serwera, dlugosc_s);
- if (res==-1) {
- perror("K: Blad polaczenia\n");
- exit(1);
- }
- write(gniazdo_k, "a", 1);
- //sleep(20);
- close(gniazdo_k);
- exit(0);
- }
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- void serwer() {
- int gniazdo_s, gniazdo_k, dlugosc_s, dlugosc_k, res;
- struct sockaddr_in adres_serwera, adres_klienta;
- //utworzenie i dowiązanie gniazda
- gniazdo_s=socket(AF_INET, SOCK_STREAM, 0);
- adres_serwera.sin_family=AF_INET;
- adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
- adres_serwera.sin_port=htons(11199); //port serwera
- dlugosc_s=sizeof(adres_serwera);
- res=bind(gniazdo_s, (struct sockaddr *) &adres_serwera, dlugosc_s);
- if (res==-1) {
- perror("S: Blad bind\n");
- exit(1);
- }
- //nasłuchiwanie i obsługa klientów
- listen(gniazdo_s, 5);
- while(1) {
- char linia[255];
- int n;
- printf("S: Czekam na klienta:\n");
- gniazdo_k=accept(gniazdo_s, (struct sockaddr *) &adres_klienta, &dlugosc_k);
- n=read(gniazdo_k, linia, 255);
- linia[n]='\0';
- printf("%s\n", linia);
- close(gniazdo_k);
- }
- }
- main() {
- pid_t pid1;
- if((pid1 = fork()) < 0)
- perror("S: Nie moge stworzyc nowego procesu\n");
- else
- if(pid1 == 0) serwer();
- else printf("S: Serwer dziala w tle\n");
- }
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <stdio.h>
- #include <stdlib.h>
- main() {
- int gniazdo_k, dlugosc_s, res;
- struct sockaddr_un adres_serwera;
- char *nazwa_gniazda="gniazdo_serwera";
- gniazdo_k=socket(AF_UNIX, SOCK_STREAM, 0);
- adres_serwera.sun_family=AF_UNIX;
- strcpy(adres_serwera.sun_path, nazwa_gniazda);
- dlugosc_s=sizeof(adres_serwera);
- //polaczenie z serwerem
- res=connect(gniazdo_k, (struct sockaddr *) &adres_serwera, dlugosc_s);
- if (res==-1) {
- perror("K: Blad polaczenia\n");
- exit(1);
- }
- write(gniazdo_k, "b", 1);
- //sleep(20);
- close(gniazdo_k);
- exit(0);
- }
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <stdio.h>
- #include <stdlib.h>
- void serwer() {
- int gniazdo_s, gniazdo_k, dlugosc_s, dlugosc_k, res;
- struct sockaddr_un adres_serwera, adres_klienta;
- char *nazwa_gniazda="gniazdo_serwera";
- //utworzenie i dowiązanie gniazda
- unlink(nazwa_gniazda);
- gniazdo_s=socket(AF_UNIX, SOCK_STREAM, 0);
- adres_serwera.sun_family=AF_UNIX;
- strcpy(adres_serwera.sun_path, nazwa_gniazda);
- dlugosc_s=sizeof(adres_serwera);
- res=bind(gniazdo_s, (struct sockaddr *) &adres_serwera, dlugosc_s);
- if (res==-1) {
- perror("S: Blad bind\n");
- exit(1);
- }
- //nasłuchiwanie i obsługa klientów
- listen(gniazdo_s, 5);
- while(1) {
- char linia[255];
- int n;
- printf("S: Czekam na klienta:\n");
- gniazdo_k=accept(gniazdo_s, (struct sockaddr *) &adres_klienta, &dlugosc_k);
- n=read(gniazdo_k, linia, 255);
- linia[n]='\0';
- printf("%s\n", linia);
- close(gniazdo_k);
- }
- }
- main() {
- pid_t pid1;
- if((pid1 = fork()) < 0)
- perror("S: Nie moge stworzyc nowego procesu\n");
- else
- if(pid1 == 0) serwer();
- else printf("S: Serwer dziala w tle\n");
- }
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <stdlib.h>
- main() {
- int gniazdo_k, dlugosc_s, res;
- struct sockaddr_in adres_serwera;
- gniazdo_k=socket(AF_INET, SOCK_STREAM, 0);
- adres_serwera.sin_family=AF_INET;
- adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
- adres_serwera.sin_port=htons(22299); //port serwera
- dlugosc_s=sizeof(adres_serwera);
- //polaczenie z serwerem
- res=connect(gniazdo_k, (struct sockaddr *) &adres_serwera, dlugosc_s);
- if (res==-1) {
- perror("K: Blad polaczenia\n");
- exit(1);
- }
- write(gniazdo_k, "c", 1);
- //sleep(20);
- close(gniazdo_k);
- exit(0);
- }
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/time.h>
- #include <sys/ioctl.h>
- void serwer() {
- int gniazdo_s, gniazdo_k, dlugosc_s, dlugosc_k, res;
- struct sockaddr_in adres_serwera, adres_klienta;
- fd_set odczyt_z_gniazd, deskryptory_gniazd;
- //utworzenie i dowiązanie gniazda
- gniazdo_s=socket(AF_INET, SOCK_STREAM, 0);
- adres_serwera.sin_family=AF_INET;
- adres_serwera.sin_addr.s_addr=inet_addr("158.75.2.10"); //aleks-2
- adres_serwera.sin_port=htons(22299); //port serwera
- dlugosc_s=sizeof(adres_serwera);
- res=bind(gniazdo_s, (struct sockaddr *) &adres_serwera, dlugosc_s);
- if (res==-1) {
- perror("S: Blad bind\n");
- exit(1);
- }
- //nasłuchiwanie i obsługa klientów
- listen(gniazdo_s, 5);
- FD_ZERO(&odczyt_z_gniazd); //zerowanie listy deskryptorow
- FD_SET(gniazdo_s, &odczyt_z_gniazd); //dodanie deskryptora gniazda serwera do nasluchu dla select
- while(1) {
- char linia[255];
- int n, l_gniazd, deskr, bajty_oczek;
- deskryptory_gniazd=odczyt_z_gniazd;
- printf("S: Czekam na klienta:\n");
- l_gniazd=select(FD_SETSIZE, &deskryptory_gniazd, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);//wlaczamy naskuch na deskryptorach
- if (l_gniazd < 1) {
- perror("S: blad select");
- exit(2);
- }
- for (deskr=0; deskr< FD_SETSIZE; deskr++) {
- if (FD_ISSET(deskr, &deskryptory_gniazd)) {
- if (deskr == gniazdo_s) { //do serwera zglosil sie nowy klient
- gniazdo_k=accept(gniazdo_s, (struct sockaddr *) &adres_klienta, &dlugosc_k);
- FD_SET(gniazdo_k, &odczyt_z_gniazd); //gniazdo klienta dodane do nasluchu przez select
- printf("S: dodaje deskryptor gniazda klienta do nasluchu select\n");
- }
- else { //klient przyslal dane przez gniazdo
- ioctl(deskr, FIONREAD, &bajty_oczek); //odczyt ile bajtow czeka w buforze do odczytu
- if (bajty_oczek == 0) {
- close(deskr); //zamkniecie gniazda
- FD_CLR(deskr, &odczyt_z_gniazd); //usuniecie deskryptora gniazda z nasluchu select
- printf("S: usuwam deskryptor gniazda klienta do nasluchu select\n");
- }
- else {//jest niezerowa ilosc bajtow do odczytu
- n=read(gniazdo_k, linia, 255);
- linia[n]='\0';
- printf("S: dane od klienta: %s\n", linia);
- }
- }
- }
- }
- //close(gniazdo_k);
- }
- }
- main() {
- pid_t pid1;
- if((pid1 = fork()) < 0)
- perror("S: Nie moge stworzyc nowego procesu\n");
- else
- if(pid1 == 0) serwer();
- else printf("S: Serwer dziala w tle\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement