linux etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
linux etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

14 Haziran 2015 Pazar

Blocking Signal with sigset_t,SIG_BLOCK linux example

#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
 int main(){
sigset_t base_mask, waiting_mask;

sigemptyset (&base_mask);
sigaddset (&base_mask, SIGINT);
sigaddset (&base_mask, SIGQUIT);

// when you doing some process , user would blocks signals
sigprocmask (SIG_BLOCK, &base_mask, NULL);
int test1=0,test2=0;
sleep(3);
// is there any signal which was waiting for  ?
sigpending (&waiting_mask);
if (sigismember (&waiting_mask, SIGINT)) {
  printf("user tried to finish process with SIGINT \n ");
  test1++;
}if (sigismember (&waiting_mask, SIGQUIT)) {
  printf("user tried to finish process with SIGQUIT \n ");
  test2++;
}

else if (sigismember (&waiting_mask, SIGTSTP)) {
   printf("user didn't try to finish process  \n ");
}

 printf(" time of INT %d " ,test1);
 printf("\n time of QUIT %d  " ,test2);
  printf("\n Signal will act like a normal \n times of INT :");
 //sigfillset(sigset_t *set)
   sigprocmask (SIG_UNBLOCK, &base_mask,NULL);;
   //signal(SIGINT,SIG_DFL);
   //signal(SIGQUIT,SIG_DFL);
   sigdelset (&base_mask, SIGINT);
   sigdelset(&base_mask, SIGTSTP);
 


 }

Change Directory chdir and getcwd example

Hello everyone , this article for my linux exam. I hope it's beneficial for every computer science student. This programme firstly shows current working directory after that takes new directory command with scanf.If you have any trouble with this topic just send me e-mail : enes065@gmail.com .


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char** argv)
{
pid_t child_pid;
 char *message;
  char s[500];
   char* cwd;
    char buff[500];
     char *directory  ;
      int ret;

int repetition;
printf("So far only one process is runing\n");
child_pid = fork();
switch (child_pid)
{
case -1:
printf("fork process failed\n");
exit(1);

case 0:
message = "J am a child";
 printf( "My working directory is %s.\n", getcwd(buff, 500));
 printf("New directory: \n");
         scanf("%s",s);
         chdir(s);

break;
default:
message = "J am a parent";

break;
}
printf( "My working directory is %s.\n", getcwd(s, 500));
}

11 Haziran 2015 Perşembe

Linux Signals - Linux Sinyaller

 Merhaba Arkadaşlar bu yazımda SIGINT ve SIGQUIT sinyallerini yakalamayı göreceksiniz. Yakalamak için önce handler dediğimiz fonksiyonu yazmanız ondan sonra signal(SIGINT,handler) biçiminde fonksiyona göndermeniz gerekiyor bunu yaptıktan sonra fonksiyon içine if yapısı koyup if(sig==SIGQUIT) gibi bir condition koyup signalinizi ayırabilirsiniz çeşidine göre .


#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

//basic version of the program displays 3 subtitles by pressing CTRL+C
//to finish the program press CTRL+\ (SIGQUIT)

char strings[3][80]={"Ha! Ha! Ha! It's only a superficial signal!\n",
"Only process %d can ignore signal %d \n",
"Ouch - that hurt!\n"};
int string_no=0;

void handler(int sig)
{
  if (sig==SIGQUIT){
    printf(strings[string_no], getpid(),sig);
  }
  else {
  printf(strings[string_no],getpid(),sig);
string_no=((string_no + 1) % 3);
  }
}

int main(int argc, char** argv){
 
  signal(SIGINT,handler);//ctlrl + c kısayollu interrupt signal
  signal(SIGQUIT,handler); ctrl + \ kısayollu quit signali

  while (true) // infinite loop
{
printf("Hello it is process: %d\n",getpid());
sleep(1);
}
  return 0;
}

/* if you want to stop program you can open new terminal and enter kill command with process id like " kill 3593 "

10 Haziran 2015 Çarşamba

Linux Parent and Child Process - Fork Function

Function fork()

#include <unistd.h>
pid_t fork(void);

fork() creates a new process by duplicating the calling process. The new process has its own identifier. It also has its own data space, which is a copy of their relevants from the parent process.

Functions getpid(), getppid()

#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);

The functions getpid() and getppid() return own identifier (PID) of the caller
ID and a parent ID.


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
  pid_t child_pid;
  char *message;
  printf("So far only one process is runing\n");
  child_pid = fork();
  switch (child_pid)
  {
  case -1:
    printf("fork process failed\n"); // if it's failed
    exit(1);
  case 0:
    message = "I am a child"; // Child process is working

  break;
  default:
    message = "I am a parent"; // Parent process is working
   
  break;
  }
for (; ; ) // infinite loop you can break with interrupt signal (ctrl + c )
{
puts(message);
sleep(2);
}
}