Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
FILE
#include<fcntl.h>
int open(char * file_name, int option_flag [,int mode])
#include <unistd.h>
int close(int ds_file)
#include <sys/types.h>
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
#include <sys/types.h>
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
PROCESSI
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *stat_loc);
pid_t waitpid(pid_t pid, int *stat_loc, int options);
#include <unistd.h>
int execl(const char *path, const char *arg0, ..., (char *)0);
int execlp(const char *file, const char *arg0, ..., (char *)0);
int execle(const char *path, const char *arg0, ..., (char *)0, char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *path, char *const argv[], char *const envp[]);
1
#include <sys/wait.h>
WIFEXITED(valore), restituisce non_zero se il processo è terminato
normalmente (con exit)
WEXITSTATUS(valore), se il processo è terminato normalmente, la funzione
ritorna il codice di uscita
SEGNALI
#include <signal.h>
void (*signal(int sig, void (*sigHandler)(int)))(int);
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
#include <unistd.h>
int pause(void);
#include <unistd.h>
unsigned int alarm (unsigned int seconds);
THREAD
#include <pthread.h>
int pthread_equal (pthread_t tid1, pthread_t tid2);
int pthread_create(pthread_t *thread, pthread_attr_t *attr,
void *(*startRoutine)(void *), void *arg);
void pthread_exit(void *valuePtr);
int pthread_join(pthread_t tid, void **valuePtr);
int pthread_cancel(pthread_t tid);
int pthread_detach(pthread_t tid);
int pthread_attr_init(pthread_attr_t *attr);
MUTEX
#include<phtread.h>
int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); //attr=NULL
int pthread_mutex_lock (pthread_mutex_t *mutex);
int pthread_mutex_unlock (pthread_mutex_t *mutex);
int pthread_mutex_destroy (pthread_mutex_t *mutex);
2
SEMAFORI
#include <sys/sem.h>
key_t ftok(char *path, int ID);
int semget(key_t key, int num_sems, int sem_flags); //sem_flags=IPC_CREAT | 0XXX
int semop(int sem_id, struct sembuf *sem_ops, size_t num_sem_ops);
struct sembuf {
short sem_num; //indice del semaforo
short sem_op; // -1 per P(), +1 per V()
short sem_flg; //SEM_UNDO
};
int semctl(int sem_id, int sem_num, int command, ...); // command: SETVAL, IPC_RMID
union semun {
int val; //si usa solo questo campo, valore per inizializzare il semaforo (SETVAL)
struct semid_ds *buf;
unsigned short *array;
};
3
if (semop(sem_id, &sem_b, 1) == -1) {
fprintf(stderr, "V() failed\n");
return(-1);
}
return(0);
}
IPC-SHARED MEMORY
#include <sys/shm.h>
key_t ftok(char *path, int ID);
int shmget(key_t key, size_t size, int shmflg); //shmflg=IPC_CREAT|0XXX
void *shmat(int shm_id, const void *shm_addr, int shmflg); //shm_addr=(void *)0, shmflg=0
int shmdt(const void *shm_addr);
int shmctl(int shm_id, int cmd, struct shmid_ds *buf); //se command=IPC_RMID allora buf=0
int msgctl(int msqid, int command, struct msqid_ds *buf); //buf =0 se command=IPC_RMID
struct msqid_ds {
struct ipc_perm msg_perm; /* Proprietà e permessi */
time_t msg_stime; /* Istante dell'ultima msgsnd() */
time_t msg_rtime; /* Istante dell'ultima msgrcv() */
time_t msg_ctime; /* Istante dell'ultima modifica */
unsigned long __msg_cbytes; /* Numero di bytes in coda */
msgqnum_t msg_qnum; /*Numero di messaggi attualmente in coda */
msglen_t msg_qbytes;/*Numero Massimo di Bytes ammessi in coda*/
pid_t msg_lspid; /* PID del processo che ha realizzato l'ultimo msgsnd() */
pid_t msg_lrpid; /* PID del processo che ha realizzato l'ultimo msgrcv() */
};
Valori di Command: specifica l'azione da intraprendere:
4
IPC_STAT, setta i valori degli attributi della struttura msqid_ds associata alla message
queue nella struttura specificata (buf)
IPC_SET, setta i valori degli attributi della struttura msqid_ds associata alla message queue
pari a quelli specificati (buf)
IPC_RMID, elimina la message queue
◦ tutti i messaggi contenuti vengono persi
IPC-SOCKETS
struct sockaddr_in {
short int sin_family; /* domain: AF_INET */
unsigned short int sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
};
struct in_addr {
unsigned long int s_addr;
};
#include <sys/socket.h>
int socket(int domain, int type, int protocol); //AF_INET, SOCK_STREAM, 0
int bind(int socket, const struct sockaddr *address, size_t address_len);
int listen(int socket, int backlog);
int accept(int socket, struct sockaddr *address, size_t *address_len);
int connect(int socket, const struct sockaddr *address, size_t address_len);
Ricordarsi che:
IP address standard 127.0.0.1 o localhost, per definire un socket dedicato a comunicazioni
interne alla macchina
inet_addr("151.97.6.4") (#include<arpa/inet.h>)
htons(port) (#include<netinet/in.h>)
5
Utility:
#include<errno.h>
errno //variabile dove viene memorizzato il codice di errore di una
System Call
void perror(const char *s);
char *strerror(int errnum);
#include<stdlib.h>
int rand (void);
Per Esempio:
r=rand()%10; //per generare un numero intero compreso tra 0 e 9