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

9 Haziran 2015 Salı

Caesar Cipher Solver - Sezar Şifresi Çözücü

This programme made with Visual Studio 2012 c++ console application .Firstly choose 0 for crypt or 1 for encrypt enter senteces  press enter and then enter key number (it means how many foward do you want ? ) I didn't add explain line if you need any help you can send me mail to enes065@gmail.com. I hope this article will be benefical for you.

Bu program Visual Studio 2012 c++ console application yapılmıştır. Şifreleme için 0 çözmek için 1 girip ardından metninizi giriniz.Anahtar rakamı girdikten sonra metniniz hazır.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

char encrypt(char c,int key2)
{

if(isalpha(c)){
c=toupper(c);
c = (((c-65)-key2) % 26) + 65;
}

cout<<c;
return c;
}

char caesar(char c,int key)
{

if(isalpha(c)){
c=toupper(c);
c = (((c-65)+key) % 26) + 65;
}

cout<<c;
return c;
}


int _tmain(int argc, _TCHAR* argv[])
{
string text; int key; int a;
string text2; int key2; int a2;
string input;

cout<<"for crypt please enter 0, for encrypt please enter 1"<<endl;
cin>>a;
switch (a)
{
case 0:
    cout << "please enter the text" << endl;
    getline(cin, text);
getline(cin, text);
cout<<"Please enter key number"<<endl;
cin>>key;
for (int i = 0; i < text.length(); i++)
{
caesar(text[i],key);
} ; break;

case 1:
cout << "please enter the text" << endl;
    getline(cin, input);
getline(cin, text2);
cout<<"please enter key number"<<endl;
cin>>key2;

for (int i = 0; i < text2.length(); i++)
{
encrypt(text2[i],key2);
}; break;

    default:cout<<"Please Enter Value"<<endl;

}

cout<<endl;
system("pause");
return 0;
}