NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/**
* @file main.c
* @brief Description
* @date 2016-11-08
* @author Pedro Figueiredo -2140260
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <pthread.h>

#include "debug.h"
#include "memory.h"
#include "args.h"
//Socket libraries
#include <sys/socket.h>
//Address libraries
#include <arpa/inet.h>

//Error handling
#define C_ERRO_PTHREAD_CREATE 1
#define C_ERRO_PTHREAD_JOIN 2
#define C_ERRO_PARAM 3
#define ERR_ARGS 4
#define ERR_SYS_CALL 5

#define LOCK(m) (assert(pthread_mutex_lock(m) == 0))
#define UNLOCK(m) (assert(pthread_mutex_unlock(m) == 0))
#define WAIT(c,m) (assert(pthread_cond_wait(c,m) == 0))
#define SIGNAL(c) (assert(pthread_cond_signal(c) == 0))
#define BROADCAST(c) (assert(pthread_cond_broadcast(c) == 0))
//Prototypes
//prototipo
void handler(int signal,siginfo_t *siginfo,void *context);
void *task(void *arg);

volatile int stop = 0;



int main(int argc, char *argv[]){
/* Disable warnings */
(void)argc; (void)argv;

int contador1=0;
int contador2=0;




// Signal handling SIGUSR1 e SIGINT
struct sigaction sa;
sa.sa_sigaction = handler;
sa.sa_flags = 0;
sa.sa_flags |= SA_SIGINFO;
sa.sa_flags |= SA_RESTART; /*recupera chamadas bloqueantes*/
sigemptyset(&sa.sa_mask);

if (sigaction(SIGUSR1, &sa, NULL) != 0) {
ERROR(ERR_SYS_CALL, "sigaction() failed");
}
if (sigaction(SIGUSR2, &sa, NULL) != 0) {
ERROR(ERR_SYS_CALL, "sigaction() failed");
}
if (sigaction(SIGQUIT, &sa, NULL) != 0) {
ERROR(ERR_SYS_CALL, "sigaction() failed");

}


printf("[INFO] PID=%d - waiting for SIGUSR1 or SIGUSR2 or SIGQUIT (finish)n", getpid());
printf("[INFO] use: kill -s SIGUSR1 %d OR kill -s %dn", getpid(), getpid() );

while (1) {
pause(); //pause waiting for signal

if (stop == 1)
{
contador1++;

pthread_t tid;
// cria uma thread + passagem de parâmetro
if ((errno = pthread_create(&tid, NULL, task, NULL) != 0))
ERROR(10, "Erro no pthread_create()!");

// espera que a thread termine
if ((errno = pthread_join(tid, NULL)) != 0)
ERROR(11, "Erro no pthread_join()!n");

}
//stuff
if (stop == 2)
{
contador2++;

pid_t pid;
pid=fork();

if (pid==0){

printf("O meu PID é: [%d]n",getpid());

//tamanho do ficheiro
FILE *fp;
fp = fopen("prog","r");
fseek(fp, 0L, SEEK_END);
int sz = ftell(fp);

printf("%d bytesn",sz);

fclose(fp);

exit(0);

}else if (pid>0) {



}else{
ERROR(1, "Erro na execucao do fork()");
}



}
if (stop == 3)
{
printf("SIGUSR1: %d receivedn",contador1);
printf("SIGUSR2: %d receivedn",contador2);
exit(0);
}


}


return 0;
}



//signal handler function
void handler(int signal,siginfo_t *siginfo,void *context)
{
(void)context;

if (signal == SIGUSR1) {
stop = 1;
}
if (signal == SIGUSR2) {
stop = 2;
}
if (signal == SIGQUIT) {

stop = 3;


printf("Detalhes:n");
printf("tPID: %ldntUID:%ldn", (long)siginfo->si_pid,
(long)siginfo->si_uid);



}
}





void *task(void *arg)
{

(void) arg;

time_t curtime;
time(&curtime);

printf("Current time = %s", ctime(&curtime));


pthread_exit(NULL);

return NULL;
}




     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.