sourcecode

Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Thursday, July 5, 2012

Append v.s. Overwrite

http://forum.ivorde.ro/linux-shell-protecting-against-accidental-output-redirection-and-file-overwrite-t26.html

When redirecting the output, using > overwrites an existing file and using >> to append to an existing file.

When I was recording the output of a series of command to a single file, I started to worry about that I could type > instead of >> by mistake and accidentally erase all the hard work I had done.

Here is the save: disable the ability to overwrite a file by setting noclobber:
$set -o
shows
noclobber    off
then
$set -o noclobber
TA-DA, files can not be overwritten anymore. I personally like this setting. If I really want to overwrite a file, I would rm it first, then create a new one.



From Starship Troopers:
http://en.wikiquote.org/wiki/Starship_Troopers_%28film%29

Ace Levy: Sir, I don't understand. What good's a knife in a nuke fight? All you have to do is press a button, sir.
Career Sergeant Zim: Put your hand on that wall, trooper. [Ace hesitates] PUT YOUR HAND ON THAT WALL! [Zim throws a knife and hits Ace's hand, pinning it to the wall] The enemy cannot press a button... if you have disabled his hand. Medic!
/***************END******************/

Copy file between servers using ssh in command line

http://www.howtogeek.com/66776/how-to-remotely-copy-files-over-ssh-without-entering-your-password/

scp is the command
$scp orig.txt user@example.com:~/dest.txt


/*************END***********/

pthread_create()

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
http://www.cs.cf.ac.uk/Dave/C

Tried to find the simplest example of using pthread_create for a beginner. The man page of pthread_create, again, is rather a reference for experts who already know how to use pthread_create.

Here I have the simplest working code without all the distractions:
#include <stdio.h>
#include <pthread.h>

void* threadFunction(){//the function is the entry point of a new thread
  printf("Catch = %d\n",22);
}

int main(){

  pthread_t threadId;//to record the thread id of the newly created thread
  //effectively pthread_t is type unsigned long int

  pthread_create(&threadId, NULL, threadFunction, NULL);
  //threadFunction, inputArg together is a function call equivalent to the
  //more familiar form: threadFunction(inputArg);

  pthread_join(threadId,NULL);
  //this function call forces the main() is waiting for the thread to complete. 
  //(for the thread to "join" back?)
  //Otherwise, the main
  //may exit before the thread finishes, thus aborts the thread prematurely

  printf("threadId = %lu\n",threadId);

  return 0;
  }
 

The thread function takes exactly one entrance parameter,  similar to main() you can have main(char argc, char** argv). It is common practice to have the entrance parameter be a struct so that complicated data can be passed into the thread.

#include <stdio.h>
#include <pthread.h>

void* threadFunction(void* threadFunctionArg){
  int* pMax = threadFunctionArg;
  printf("Double the input =%d\n",*pMax * 2);
}


int main(){
  int num=5;//some information to pass to the new thread

  pthread_t threadId;//to record the thread id of the newly created thread
  //effectively pthread_t is type unsigned long int

  int threadStatus;//to record the return status of the new thread
  //0 means good, otherwise an error number is recorded.

  void* inputArg = &num;//used to pass into the thread function

  threadStatus = pthread_create(&threadId, NULL, threadFunction, inputArg);
  //threadFunction, inputArg together is a function call equivalent to the
  //more familiar form: threadFunction(inputArg);

  pthread_join(threadId,NULL);
  //the main() is waiting for the thread to complete. Otherwise, the main
  //may exit before the thread finishes, thus aborts the thread prematurely

  printf("threadId = %lu\n",threadId);

  printf("threadStatus = %d\n",threadStatus);

  return 0;
  }
/********************END******************/

Tuesday, July 3, 2012

man page

Often I saw "refer to man page write(2)". When I type
$man write(2)
I get
bash: syntax error near unexpected token `('
Well, simply enough, the right syntax for pointing a certain section is
$man 2 write
/**************END**********************/

Thursday, June 28, 2012

How do I know which Linux version I am using

http://serverfault.com/questions/3331/how-do-i-find-out-what-version-of-linux-is-running
http://www.cyberciti.biz/faq/howto-find-out-what-kernel-version-running/
$ uname -mrsn

There is no cross-distribution way. However:
  • Redhat and friends: Test for /etc/redhat-release, check contents
  • Debian: Test for /etc/debian_version, check contents
  • Mandriva and friends: Test for /etc/version, check contents
  • Slackware: Test for /etc/slackware-version, check contents
Etc. Generally speaking, check for /etc/*-release and /etc/*-version.

/************END*****************/