S60 Open C
Introduction to Open C

Introduction to Open C

Table of Contents

Symbian OS vs. UNIX
Hello World
File I/O
Thread creation

 


Symbian OS vs. UNIX

The previous section gave an overview of the amount of work that, needs to be done when programming on Symbian and UNIX environments. This section deals with the more general scenario that will be used frequently in a typical operating system.

 


Hello World

Assuming that the user is new to the UNIX environment, writing a simple program that displays a string on a console does not take more than a day’s effort. Its simply 4-5 lines of code as shown below:

#include <stdio.h>
int main(int argc, char** argv) {
       printf(“Hello world\n”);
       return 0;
   }

The code contains a main() , one printf statement, and one #include< >. By convention UNIX applications will map descriptor 0 with standard input (STDIN), descriptor 1 with standard output (STDOUT), and descriptor 2 with standard error (STDERROR) of the process. So, there is no need to create/open standard input/output/error for a process before using them. Notice also that, main( ) takes two arguments. These arguments will be read from the command prompt before invoking main( ) and will be passed to main. These arguments are not mandatory; they can be ignored. No additional work is needed for having these arguments.

If we attempt to display a string on a console using Symbian OS, a lot of effort goes into collecting the materials, and for coding. The first time the developer may find the code a little complex to understand.

#include <e32std.h>
#include <e32cons.h>

int HelloWorldL()
       {
       CConsoleBase* console = Console::NewL(_L("Hello World"), TSize( KConsFullScreen, KConsFullScreen));
       console->Printf(_L(“Hello World\n”));
       delete console;
       return 0;
       }

TInt E32Main()
       {
       __UHEAP_MARK;
       CTrapCleanup* cleanup = CTrapCleanup::New();
       TInt retVal = KErrNone;
       if (cleanup)
           {
           TRAP(retVal, HelloWorldL());
           __ASSERT_ALWAYS(!retVal, User::Panic(_L("Hello-World PANIC"), retVal));
           //Destroy cleanup stack
           delete cleanup;
           }
       __UHEAP_MARKEND;
       return retVal;
       }

The code above has some header inclusions, two functions, and some complicated codes. In Symbian OS, the entry function for an EXE is E32Main (not main as in UNIX). The following actions are done to print a message on a console:

  1. Mark the heap using __UHEAP_MARK (not mandatory).
  2. Create the cleanup stack.
  3. Have a top-level TRAP for the function HelloWorldL function.
  4. In the HelloWorldL function, create a console object.
  5. Do print on the created console object.
  6. Delete the console.
  7. Delete the cleanup stack.
  8. Unmark the heap (not mandatory if the heap is not marked).

In Symbian OS, a console is not created by default; it has to be created explicitly. Symbian OS does not give a rich set of APIs needed to perform I/O operations on a console. The I/O operations that can be performed on a console in Symbian OS are limited to :

  • printing strings and reading one character at a time
  • getting cursor position

In case we are interested in command line arguments, it takes some more additional works. We have to read explicitly from the command prompt by using the APIs provided by Symbian OS.

 


File I/O

This section compares the ease of programming in the Open C environment against programming on Symbian OS.

/* File I/O in UNIX environment */
  FILE* fp = fopen(“file.txt”, w);
  if (fp) {
     fprintf(fp, “Write some data”);
     fclose();
  }
//File I/O in Symbian OS
  RFs fSession;
  User::LeaveIfError(fSession.Connect());
  RFile file;
  ret = file.Open(fSession, _L(“file.txt”), EFileWrite);
  if (ret) 
     {
     ret = file.Create(fSession, _L(“file.txt”), EFileWrite);
     }
  if (!ret) 
     {
     file.Write(_L8(“Write some data”));
     }
  file.Close();
  fSession.Close();

The example above shows the complexity of the code in Symbian OS for doing simple file I/O. In addition to coding complexity, we need to include and link with :

  • efsrv.lib for any file-related operation
  • esock.lib for network-related operation
  • commdb.lib for selecting IAPs
Since most of the resources under Symbian OS are accessed through client/server IPC, we need to connect with the corresponding server before doing any operation and close the session on completion of the operation. For example:
  • connect with the file server before file operations
  • connect with the socket server for socket-related operation
  • connect to the communication server before serial communication operation
Open C eliminates all these operations. Open C allows the user to code, as if it were done for the UNIX or Linux environment and lets the user link the application with Open C.

 


Thread creation

This section deals with thread creation in the UNIX and Symbian OS environments.

/* Thread creation in UNIX environment*/
   void* ThreadEntryPoint( void* aParam );
   …
   int exitReason = 0;
   int retVal  = 0;
   pthread_t threadID = 0;
   pthread_attr_t threadAttr;
   pthread_attr_init( &threadAttr );
   retVal = pthread_create( &threadID, &threadAttr, ThreadEntryPoint, (void*)NULL );
   if(retVal == 0) {
       retVal = pthread_join(threadID1, (void**)&exitReason );
   }
// Thread creation in Symbian OS
  TInt ThreadEntryPoint( TAny* aData );
  …
  RThread thread;
  thread.Create(_L("MY-THREAD"), ThreadEntryPoint, 4096, NULL, NULL);
  TRequestStatus stat;
  thread.Logon(stat);
  //Start executing the thread.
  thread.Resume();
  //Wait for the thread to die.
  User::WaitForRequest(stat);
  exitReason = thread.ExitReason();
  thread.Close();

In addition the difference in the code for creating threads using pthread_create and RThread::Create, a thread created using RThread::Create also expects the ThreadEntryPoint function to create its own vcleanup stack and a top-level TRAP, if required. Else, the thread may crash or panic. But this does not have to be done for a thread created using pthread_create. pthread_create does that for the developer.

Give feedback of this section


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.