Sei sulla pagina 1di 6

Prontuario API

Questo documento deve essere stampato e portato dallo studente


alla prova di esame al calcolatore per poter essere consultato.
E’ l’unico documento consultabile durante la prova di esame al
calcolatore.

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;
};

Funzioni che operano su Array di Semafori System V:

int SEM_SET(int sem_id, int sem_num, int sem_val){


union semun sem_union;
sem_union.val = sem_val;
if (semctl(sem_id, sem_num, SETVAL, sem_union) == -1) return(-1);
return(0);
}

void SEM_DEL(int sem_id, int sem_num){


union semun sem_union;
if (semctl(sem_id, sem_num, IPC_RMID, sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore\n");
}

int SEM_P(int sem_id, int sem_num){


struct sembuf sem_b;
sem_b.sem_num = sem_num;
sem_b.sem_op = -1; /* P() */
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1) {
fprintf(stderr, "P() failed\n");
return(-1);
}
return(0);
}

int SEM_V(int sem_id, int sem_num){


struct sembuf sem_b;
sem_b.sem_num = sem_num;
sem_b.sem_op = 1; /* V() */
sem_b.sem_flg = SEM_UNDO;

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

IPC- MESSAGE QUEUE


#include <sys/msg.h>
key_t ftok(char *path, int ID);
int msgget(key_t key, int msgflg); //msgflg=IPC_CREAT|0XXX
int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg); //msgflg=IPC_NOWAIT/0
struct my_message {
long int message_type;
/* Il tipo di dati che si vuole trasmettere */
}
int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);
//msgflg=IPC_NOWAIT/0
//msgtype, è un long int che permette di scegliere se:
 valore = 0, viene prelevato il primo dato disponibile in coda (FIFO)
 valore = n>0, viene prelevato, se esiste, il primo dato con il tipo (long int
message_type) avente valore corrispondente
 valore = -n, viene prelevato, se esiste, il primo dato con tipo pari a n o
inferiore

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

Potrebbero piacerti anche