Browsing all articles in tips
Jan
31

Whether should i use processes or threads in Linux?

While designing any system usually we might confuse between thread and process usage . We think should we use thread or process for our requirement. Which will be the best choice.?

 

So Here are some guidelines to help you……

 

 

  • All threads inside a program always run from the same executable binary. Where a child process , may run a from different executable binary by calling an exec function.
  • If one process crashes or has a buffer overrun, it does not affect any other process at all, whereas if a thread crashes, it takes down all of the other threads in the process because threads share the same virtual memory space and other resources. read more
Jan
30

Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C programming

 If you all ready know the difference between file descriptor and file pointer then good otherwise please read this post

Whats is difference between file descriptor and file pointer?

 

 

File descriptor to file pointer :

for converting file descriptor to file pointer in linux fdopen() function is used

#include <stdio.h
FILE *fdopen(int fildes, const char *mode);

fildes is file descriptor

mode is the character value which indicate the mode of file operation (same as we use in fopen() function)

Example :

#include <stdio .h>
#include <fcntl .h>
 
int main(){
int fd;
FILE *fp;
fd = open("shareprogrammingtips.txt",O_WRONLY | O_CREAT | O_TRUNC);
if(fd<0)
{
   printf("open call fail");
   return -1;
}
 
fp=fdopen(fd,"w");
fprintf(fp,"we got file pointer fp bu using File descriptor fd");
fclose(fp);
close(fd);
return 0;
}
</fcntl></stdio>

read more

Jan
5

How can i detect Windows or Linux in C,C++ programming?

 

Sometimes we need to write platform independent  code which works in windows and linux machine without giving any flag or any modification in code or make file manually.

So in such case i usually use compiler generated macro to check the platform .

 

In Linux all c and c++ compiler define macro like  linux, In Windows all c and c++ compiler define macro like _WIN32 

so we can use this macro to separate code for linux and windows see belows example code

read more

Jan
5

How can I get what my main function has returned in Linux?

 

In a C program if we want to give some input from terminal then we can give it by:

int main(int argc, char *argv[])

In the same way, if we want to get return value of main() function then how can we get it?

 

In each main() we write return 1 or return 0; how can we know what my main() has returned at terminal?

read more

Dec
14

Why fopen() doesn’t get fail by passing directory name in argument?

Author JIGAR PATEL    Category c, linux, Tips, tips     Tags

Once i have made one small application where i was passing full file name as command line argument in my application where i am going to fopen() that file BUT once i have just passed any directory name instad of file name still my fopen() does not get fails…!!

#include&lt;stdio.h&gt;
#include &lt;errno.h&gt;
 
int main()
{
errno = 0;
FILE *fb = fopen("/home/jeegar/","r");
if(fb==NULL)
    printf("its null");
else
    printf("working");
 
printf("Error %d \n", errno);
 
}

output is

 workingError 0

here fb is not going null and also not getting any error..

read more

Dec
14

Whats is difference between file descriptor and file pointer?

Author JIGAR PATEL    Category c, linux, Tips, tips     Tags

After having 1 year of experience in c programming when someone has asked this to me i was surprised that why i dont know this…!!

Well when i will going to interview someone in someday this would be my 1st question to him/her…!!!

File Descriptor :  

  “A file descriptor is a low-level integer “handle” used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems.”

 int fd;
 fd = open(path, O_CREAT | O_RDONLY, 0644);
 close(fd);

This way you are using that is file-descriptor. In short,  file-descriptor is

  1. Low/Kernel level handler
  2. passe to read() and write() of UNIX System Calls
  3. Doesn’t include buffering and such features
  4. Less portable and lacks efficiency
  5. It’s some what OS specific so its not part of standard c programming language.
Nov
6

how to increase stack size in linux.

Author saurabh    Category linux, tips     Tags

Normally you would set the stack size early on, e,g, at the start of main(), before calling any other functions. Typically the logic would be:

  • call getrlimit to get current stack size
  • if current size < required stack size then
    • call setrlimit to increase stack size to required size

In C that might be coded something like this:
read more

Nov
6

Equivalent to getch() & getche() in Linux

Once in one situation i need to use getch() and getche() function in Linux based application so i have searched a lot for Equivalent header file for conio.h in Linux,But i came to know there is not any header file/library equivalent to conio.h in linux.

I want to make a switch case base menu where the user will give his option just by pressing one key & process should be moved ahead. I don’t want to let user to press ENTER after pressing his choice.

Somebody has suggest me very magical code for getch() and getche() function for linux which is i am sharing here. read more

Oct
17

Top/Best C/C++ IDE for Windows & Linux

Author JIGAR PATEL    Category c, c++, linux, Tips, tips, tips     Tags

How much time gone in project design & building logic that much time also gone in writing code So if you have better IDE then you can work faster and easily. So i am going to introduce you most famous IDE for C/C++ programming on Linux & windows.

1>Eclipse CDT (C/C++ Development Tooling)

read more

Oct
10

Find and replace command for whole directory in Linux

In many case most of the projects have so many files in so many different folder. So at some point you want to do find and replace for particular word in your whole project or whole directory in that case this command will help you.

In linux for doing this there is no single command but we can design one new command by combination of several.

find /path/to/directory -type f -exec sed -i.bak 's/oldword/newword/g' {} \;

read more

Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

36 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link